简体   繁体   English

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

[英]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 . 您的请求模型仅适用于eventbody

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.

相关问题 通过 API 网关进行测试时,AWS lambda 和 python 出现错误 - Error in AWS lambda with python when testing through API Gateway Python 快速 api 获取内部服务器错误 - Python fast api getting internal server error 使用API​​网关尝试AWS Python Lambda函数中的访问参数时出错 - Error when trying access parameters in AWS Python Lambda function using API Gateway api网关和lambda分页 - api gateway and lambda pagination 获取“内部服务器错误”502:错误的网关错误 - Getting "Internal Server Error" 502:Bad Gateway Error 调用 InvokeEndpoint 操作时发生错误 (ModelError):收到服务器错误 (500)。 从 lambda+ api 网关调用端点 - An error occurred (ModelError) when calling the InvokeEndpoint operation: Received server error (500). Calling the endpoint from lambda+ api gateway python + nginx内部服务器错误 - python + nginx internal server error 如何使用boto3创建api-gateway时添加命令“使用Lambda代理集成” - how to add the command 'Use Lambda Proxy integration' while creating api-gateway using boto3 设置自定义 header 与 API 网关非代理 lambda 和二进制 Z78E6221F6393D1356681DB3 - Setting custom header with API gateway non-proxy lambda and binary output 为什么通过python请求调用rest api时出现500 Internal Server Error? - Why am I getting 500 Internal Server Error when calling post rest api via python request?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM