通过API Gateway v Lambda Console调用lambda函数时,请求主体序列化差异 [英] Request body serialization differences when lambda function invoked via API Gateway v Lambda Console
问题描述
我在AWS API Gateway中设置了一个简单的API.设置为通过API网关代理集成来调用Python 2.7 lambda函数.
I have a simple API set up in AWS API Gateway. It is set to invoke a Python 2.7 lambda function via API Gateway Proxy integration.
我遇到一个奇怪的错误,因为在本地调用并通过lambda测试控制台(而不是通过curl或Postman)调用时,lambda起作用(正确处理了主体并更新了数据库).
I hit a strange error in that the lambda worked (processed the body correctly and updated a DB) when invoked locally and through the lambda test console, but not through curl or Postman.
结果表明,当通过lambda测试控制台调用时,event['body']
对象作为dict
通过.通过HTTP客户端调用时,它以字符串(Unicode
)的形式出现.
Turns out that, when invoked through the lambda test console, the event['body']
object is coming through as a dict
. When called via an HTTP client, it's coming through as a string (Unicode
).
我当然可以解决它,但我想了解它,并且我也希望使用合适的Python对象.我也希望能够使用lambda测试控制台,但是目前我不能,因为它以不同的方式传递其输入.
I can work around it of course, but I'd like to understand it, and I'd also prefer a proper Python object. I'd also like to be able to use the lambda test console, but currently I can't as it passes its input differently.
是否缺少配置开关,它将迫使API Gateway将请求主体(以及所有其他参数)序列化为python dict
或适当的对象?关于文档的详细信息所传递的内容是稀疏的,说明:
Is there a configuration switch I'm missing which will force API Gateway to serialize the request body (as well as all other params) as a python dict
or proper object? The documentation on the specifics of what is passed is sparse, stating:
event – AWS Lambda使用此参数将事件数据传递到处理程序.此参数通常是Python dict类型.它也可以是list,str,int,float或NoneType类型.
event – AWS Lambda uses this parameter to pass in event data to the handler. This parameter is usually of the Python dict type. It can also be list, str, int, float, or NoneType type.
我知道此内容涵盖了我所看到的内容,但效果不完全.
I get that this blurb covers what I'm seeing, but it's not exactly helpful.
推荐答案
当您在本地或通过Lambda控制台调用lambda时,您是直接调用该lambda的,因此您的lambda会准确地收到 正在发送.
When you invoke the lambda locally or through the Lambda console, you are invoking that lambda directly and so your lambda receives exactly what you're sending.
当您通过API网关调用它时,API网关会根据您的HTTP请求为您创建event
对象.它添加了HTTP标头,路径,查询字符串,有效负载等.
When you invoke it through API Gateway, API Gateway creates the event
object for you based on your HTTP request. It adds the HTTP headers, path, query strings, payload, etc.
以下是您从API网关调用中获得的event
的摘要:
Here's a summary of what you're getting as an event
from an API Gateway invocation:
{
"resource": "Resource path",
"path": "Path parameter",
"httpMethod": "Incoming request's method name"
"headers": {Incoming request headers}
"queryStringParameters": {query string parameters }
"pathParameters": {path parameters}
"stageVariables": {Applicable stage variables}
"requestContext": {Request context, including authorizer-returned key-value pairs}
"body": "A JSON string of the request payload."
"isBase64Encoded": "A boolean flag to indicate if the applicable request payload is Base64-encode"
}
参考:如您所见,body
将作为string
发送给您,您可以使用json.loads()
进行解析.
As you can see, the body
will be sent to you as a string
which you can parse using json.loads()
.
这篇关于通过API Gateway v Lambda Console调用lambda函数时,请求主体序列化差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!