API网关-Lambda代理-Python内部服务器错误

时间:2018-12-08 19:39:12

标签: python-3.x aws-lambda

我在解析python 3.7中的事件中的输入数据时遇到问题。

def lambda_handler(event, context):
 image = event['image']
 siteid = int(event['siteid'])
 camid = int(event['camid'])

错误:

Lambda execution failed with status 200 due to customer function error: 'image'.

方法请求模型:

{
  "$schema": "http://json-schema.org/draft-04/schema#",
  "title": "UploadModel",
  "type": "object",
  "properties": {
    "image": { "type": "string" },
    "siteid": { "type": "string" },
    "camid": { "type": "string" }
    }
}

使用Lambda代理集成:打开

使用简单的输入数组,可以直接在lambda控制台中正常运行

{
    "image": "xxxx"
    "siteid": 2,
    "camid": 1
}

响应功能:

def response(message, status_code):
    return {
        "statusCode": str(status_code),
        "body": json.dumps(message),
        "headers": {
            "Content-Type": "application/json",
            "Access-Control-Allow-Origin": '*'
            },
        }

1 个答案:

答案 0 :(得分:0)

您假设event对象的形状错误。

使用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"
}

参考:https://docs.aws.amazon.com/apigateway/latest/developerguide/set-up-lambda-proxy-integrations.html#api-gateway-simple-proxy-for-lambda-input-format

您的请求模型仅适用于body中的event

为说明这一点,请尝试使用以下处理程序,该处理程序将event作为响应返回:

def lambda_handler(event, context):
    return {
        "statusCode": str(status_code),
        "body": json.dumps(message),
        "headers": {
            "Content-Type": "application/json",
            "Access-Control-Allow-Origin": '*'
            },
        }