[英]API Gateway - Lambda Proxy - Python Internal server error
I have issues parsing the input data from event in Python 3.7. 我在解析python 3.7中事件的输入数据时遇到问题。
def lambda_handler(event, context):
image = event['image']
siteid = int(event['siteid'])
camid = int(event['camid'])
Error: 错误:
Lambda execution failed with status 200 due to customer function error: 'image'.
Method request model: 方法请求模型:
{
"$schema": "http://json-schema.org/draft-04/schema#",
"title": "UploadModel",
"type": "object",
"properties": {
"image": { "type": "string" },
"siteid": { "type": "string" },
"camid": { "type": "string" }
}
}
Use Lambda Proxy integration: ON 使用Lambda代理集成:开
It works fine directly from the lambda console with a simple input array: 通过简单的输入数组,可以直接从lambda控制台正常运行:
{
"image": "xxxx"
"siteid": 2,
"camid": 1
}
Response function: 响应功能:
def response(message, status_code):
return {
"statusCode": str(status_code),
"body": json.dumps(message),
"headers": {
"Content-Type": "application/json",
"Access-Control-Allow-Origin": '*'
},
}
You are assuming the wrong shape for the event
object. 您假设
event
对象的形状错误。
When you use Lambda Proxy Integration, the event
takes the following shape... 当您使用Lambda代理集成时,该
event
具有以下形状...
{
"resource": "Resource path",
"path": "Path parameter",
"httpMethod": "Incoming request's method name"
"headers": {String containing incoming request headers}
"multiValueHeaders": {List of strings containing incoming request headers}
"queryStringParameters": {query string parameters }
"multiValueQueryStringParameters": {List of 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"
}
Reference: https://docs.aws.amazon.com/apigateway/latest/developerguide/set-up-lambda-proxy-integrations.html#api-gateway-simple-proxy-for-lambda-input-format 参考: https : //docs.aws.amazon.com/apigateway/latest/developerguide/set-up-lambda-proxy-integrations.html#api-gateway-simple-proxy-for-lambda-input-format
Your request model is only applicable to the body
of event
. 您的请求模型仅适用于
event
的body
。
To illustrate this, try using this handler that returns the event
back as a response: 为了说明这一点,请尝试使用以下处理程序,该处理程序将
event
作为响应返回:
def lambda_handler(event, context):
return {
"statusCode": str(status_code),
"body": json.dumps(message),
"headers": {
"Content-Type": "application/json",
"Access-Control-Allow-Origin": '*'
},
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.