0
我試圖通過Lambda函數(通過無服務器構建)發回JSON響應,但它是錯誤的。格式錯誤的Lambda代理響應:通過無服務器部署python端點
我創建的響應:
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網關調用,這是我得到的日誌:
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網關正在尋找的內容。
它可能不是JSON的問題。你能嘗試用空響應調用Lambda嗎?只是爲了確保它是因爲許可問題 –
CloudWatch日誌告訴你什麼?那是你的實際代碼的樣子嗎?如果是這樣,那麼你的Python代碼由於不正確的縮進而出現語法錯誤。 – dashmug
@dashmug我在原帖中包含了日誌。我收到「格式錯誤的Lambda代理響應」錯誤。 – Chase