如何使用Lambda代理集成激活功能将CORS标头添加到AWS API Gateway响应 [英] How to add CORS header to AWS API Gateway response with lambda proxy integration activate

查看:68
本文介绍了如何使用Lambda代理集成激活功能将CORS标头添加到AWS API Gateway响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将lambda用作具有lambda代理集成的AWS API Gateway的后端,并希望将CORS添加到响应标头中.

I use lambda as backend for AWS API Gateway with lambda proxy integration and want to add CORS into response header.

根据文档:

http://docs.aws.amazon.com/apigateway/latest/developerguide/how-to-cors.html

但是,由于代理集成已禁用集成响应,因此必须依赖后端返回Access-Control-Allow-Origin标头.

However, you must rely on the back end to return the Access-Control-Allow-Origin headers because the integration response is disabled for the proxy integration.

如何使用Python在lambda函数中对其进行编程.

How can I program it in my lambda function with Python.

推荐答案

您需要在api网关中添加方法"options"并使用代理lambda ... return

you need to add a method "options" to your api gateway and using a proxy lambda... return

result.headers = { "Access-Control-Allow-Origin": "domain.com" }

因此,当浏览器首次调用服务器选项时,它将返回CORS标头.

so when the browser will first call options to your server it will return the CORS headers.

问题是,默认情况下,您的lambda方法将被称为"any"方法,因此您需要更改默认方法以获取,发布或任何您需要的东西

the thing is that, by default your lambda method will be called for "any" method, so you need to change the default one to get,post or whatever you need

注意:您也可以使用相同的方法,如any或options,get,post,如果它是一个options调用,则仅返回状态200和cors标头.这取决于您是否使用get,post等的auth方法

note: you could also use the same method, like any or options,get,post and if it is a options call, only return status 200 and the cors header. it depends if you are using or not an auth method for get,post,etc

如果您只是在使用lambda而没有任何奇怪的地方,则Lambda控制台中有一个启用CORS"选项

there is an option in Lambda console "Enable CORS" if you are just using lambda with nothing strange

这篇关于如何使用Lambda代理集成激活功能将CORS标头添加到AWS API Gateway响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆