简体   繁体   English

使用 AWS 网关 API 代理从 Python AWS Lambda 重定向

[英]Redirect from a Python AWS Lambda with AWS Gateway API Proxy

Posting here because I just can't get a redirect working.发帖在这里是因为我无法使重定向正常工作。 Using AWS API Gateway linked to a Python Lambda function as a proxy just returns the response and header json. Here is the code使用 AWS API 网关链接到 Python Lambda function 作为代理只返回响应和 header json。这是代码

import json

def lambda_handler(event, context):
    response = {}
    response["statusCode"]=301
    response["headers"]=[{"key": 'Location',"value": 
     'https://www.google.com'}]
    data = {}
    response["body"]=json.dumps(data)
return response

Any help will be appreciated?任何帮助将不胜感激?

Thanks谢谢

Mixed documentation on the web which was confusing. 网络上的混合文档令人困惑。 The syntax for specifying the redirect using Location needs to be the following when using Python: 使用Python时,使用“位置”指定重定向的语法必须为以下语法:

import json

def lambda_handler(event, context):   
    response = {}
    response["statusCode"]=302
    response["headers"]={'Location': 'https://www.google.com'}
    data = {}
    response["body"]=json.dumps(data)
    return response

a bit less line, with the same output少一点线,同output

def handler(event, context):
    response = {
        "headers": {"Location": "https://www.google.com", },
        "statusCode": 301,
    }

    return response

I'll prefix this by saying that when I copied this code into a Lambda function, added an API Gateway to it using the settings that made sense to me, and tested from a browser and curl, I got the correct redirect.我会在前面说,当我将此代码复制到 Lambda function 时,使用对我有意义的设置向其添加 API 网关,并从浏览器和 curl 进行测试,我得到了正确的重定向。 Which is expected, the code looks right and conforms to the specification in the documentation.这是预期的,代码看起来正确并且符合文档中的规范。

So I spent some time fiddling with settings in Lambda and in API Gateway to try to break it;所以我花了一些时间摆弄 Lambda 和 API 网关中的设置,试图破解它; plus Googling around to see how others have had it not work.加上谷歌搜索,看看其他人是如何让它不起作用的。

The general Inte.net consensus in 2021 (time of original post) is that there was a setting "Use Lambda proxy integration" in the API Gateway that needed to be turned on for API Gateway to interpret the returned JSON correctly, and this was not the default. 2021 年(发帖时间)普遍的互联网共识是 API 网关中有一个设置“使用 Lambda 代理集成”,需要为 API 网关打开才能正确解释返回的 JSON,而这不是默认值。 I can't find that setting in the console today in that format, but when you create an API Gateway "API" you select integrations, the first one in the list is Lambda. Selecting that sets up the integration correctly for interpreting the JSON (in either v1 or v2 format).我今天在控制台中找不到该格式的设置,但是当您创建 API 网关“API”时,您 select 集成,列表中的第一个是 Lambda。选择正确设置集成以解释 JSON( v1 或 v2 格式)。

If you are working in an older already-configured API Gateway endpoint, I'd suggest looking for the "Use Lambda proxy integration" setting, followed by setting up the API Gateway with the "Lambda" integration setting if it is the new-style interface.如果您使用的是已配置的旧 API 网关端点,我建议您寻找“使用 Lambda 代理集成”设置,然后使用“Lambda”集成设置设置 API 网关(如果它是新式的)界面。

If anyone is experiencing this issue right now (I presume so, since there is a current bounty on the question) then comment or update with details of how your API Gateway is set up, and I might be able to point you at where it differs.如果有人现在遇到这个问题(我想是这样,因为这个问题目前有赏金)然后评论或更新你的 API 网关是如何设置的细节,我也许能指出你不同的地方.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 API 网关 HTTP 与 aws-sam 的代理集成(不是 Lambda 代理) - API Gateway HTTP Proxy integration with aws-sam (NOT Lambda Proxy) AWS Lambda 和 API 网关 NodeJs 的格式错误的 Lambda 代理响应 - Malformed Lambda proxy response with AWS Lambda and API Gateway NodeJs AWS Api Gateway Lambda代理集成,如何从客户端请求中获取源端口 - AWS Api Gateway Lambda proxy integration, how to get source port from client request SAM AWS Lambda Python:网关 api - 处理二进制形式上传 - SAM AWS Lambda Python: gateway api - handle binary form upload AWS LAMBDA api 网关错误“格式错误的 Lambda 代理响应”状态错误 502 - AWS LAMBDA api gateway error “Malformed Lambda proxy response” status error 502 AWS - 从公共 API 网关路由到 VPC 内 lambda - AWS - Route from public API Gateway to in-VPC lambda 如何通过代理集成从 AWS API 网关提供二进制数据? - How to serve binary data from AWS API Gateway with proxy integration? 如何在代理和 API 网关后面将 API 请求 (AWS SigV4) 签名到 Lambda? - How do I sign API requests (AWS SigV4) to Lambda behind Proxy & API Gateway? aws api网关和lambda函数超时问题 - Aws api gateway and lambda functions timeout problem 通过 API 网关将文件传递给 AWS Lambda - Pass a file to AWS Lambda via API Gateway
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM