python - Lambda 代理响应格式错误 : deploying python endpoints through serverless

标签 python amazon-web-services aws-lambda aws-api-gateway serverless-framework

我尝试通过 Lambda 函数(通过无服务器构建)发回 JSON 响应,但出现错误。

我正在创建响应:

response = {}
response["file_name"] = file_name
response["status"] = status
response["description"] = message
response["data"] = data 
return {
    "isBase64Encoded": False,
    "statusCode": 200,
    "headers": {
        "Content-Type": "application/json",
    },
    "body": json.dumps(response)
}

但 AWS 仅返回:

{
    "message": "Internal server error"
}

我正在打印该响应对象,它是:

{'isBase64Encoded': False, 'statusCode': 200, 'headers': {'Access-Control-Allow-Origin': '*', 'Content-Type': 'application/json'}, 'body': '{"file_name": "upload-4660557513950187006.csv", "status": "success", "description": "Success.", "data": ""}'}

当我通过 API Gateway 调用时,这是我收到的日志:

Thu Oct 05 16:21:15 UTC 2017 : Endpoint request body after transformations: {"resource":"/parse","path":"/parse","httpMethod":"POST","headers":null,"queryStringParameters":null,"pathParameters":null,"stageVariables":null,"requestContext":{"path":"/parse","accountId":"317910044022","resourceId":"xik9xe","stage":"test-invoke-stage","requestId":"test-invoke-request","identity":{"cognitoIdentityPoolId":null,"accountId":"317910044022","cognitoIdentityId":null,"caller":"AIDAJGNM4CLIWDKHDDV2U","apiKey":"test-invoke-api-key","sourceIp":"test-invoke-source-ip","accessKey":"ASIAJHGYEQDHKLLRWL6A","cognitoAuthenticationType":null,"cognitoAuthenticationProvider":null,"userArn":"arn:aws:iam::317910044022:user/chase","userAgent":"Apache-HttpClient/4.5.x (Java/1.8.0_131)","user":"AIDAJGNM4CLIWDKHDDV2U"},"resourcePath":"/parse","httpMethod":"POST","apiId":"3gvwsa0cj2"},"body":"{\n\t\"file_name\": \"upload-4660557513950187006.csv\",\n\t\"vendor\": \"\"\n}","isBase64Encoded":false}
Thu Oct 05 16:21:15 UTC 2017 : Sending request to https://lambda.us-east-1.amazonaws.com/2015-03-31/functions/arn:aws:lambda:us-east-1:317910044022:function:PyParsingService-dev-parse/invocations
Thu Oct 05 16:21:16 UTC 2017 : Received response. Integration latency: 416 ms
Thu Oct 05 16:21:16 UTC 2017 : Endpoint response body before transformations: "{\"isBase64Encoded\": false, \"statusCode\": 200, \"headers\": {\"Access-Control-Allow-Origin\": \"*\", \"Content-Type\": \"application/json\"}, \"body\": \"{\\\"file_name\\\": \\\"upload-4660557513950187006.csv\\\", \\\"status\\\": \\\"success\\\", \\\"description\\\": \\\"Success.\\\", \\\"data\\\": \\\"\\\"}\"}"
Thu Oct 05 16:21:16 UTC 2017 : Endpoint response headers: {x-amzn-Remapped-Content-Length=0, x-amzn-RequestId=36059915-a9e9-11e7-8444-438990db7407, Connection=keep-alive, Content-Length=317, Date=Thu, 05 Oct 2017 16:21:15 GMT, X-Amzn-Trace-Id=root=1-59d65bfb-7000702549081b6682b894a9;sampled=0, Content-Type=application/json}
Thu Oct 05 16:21:16 UTC 2017 : Execution failed due to configuration error: Malformed Lambda proxy response
Thu Oct 05 16:21:16 UTC 2017 : Method completed with status: 502

对 Python 不太有经验 - 任何帮助将不胜感激。根据this响应需要针对 API 网关进行正确格式化。不确定为什么我的响应不符合 API Gateway 的要求。

最佳答案

我发现我做错了什么。 由于配置错误而执行失败:格式错误的 Lambda 代理响应错误引用了 Lambda 尝试通过 API Gateway 发回的响应。无服务器自动使用 Lambda 代理集成。当我查看响应时,我注意到基本响应级别有额外的转义(例如, "{\"isBase64Encoded\": false,\"statusCode\": 200, ...} ),我相信这触发了错误。果然,当我深入研究时,我意识到我在创建响应后转储 JSON (json.dumps()),这引发了错误.

感谢您的回复。我的无意识错误。

关于python - Lambda 代理响应格式错误 : deploying python endpoints through serverless,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46577881/

相关文章:

php - Elastic Beanstalk - PHP/强制 HTTPS

ios - 适用于 iOS 应用程序的亚马逊服务(db+REST webservices)

aws-lambda - Quarkus AWS Lambda 无效入口点错误 : fork/exec/var/task/bootstrap: exec format error Runtime. InvalidEntrypoint

python - 如何从具有不同长度列表的字典创建字典列表

python - optparse() : Input validation

python - 使用python在excel中绘制一个带有条件的单元格

python - 如何在 tensorflow 中沿轴对二维张量进行排序?

amazon-web-services - 缺少 AWS Codebuild 运行时

node.js - 使用 Nodejs Sharp 模块时出错。模块解析失败 : Unexpected character '' (1: 0)

aws-lambda - 如何在单击按钮以节流的情况下取消 lambda 函数的节流