![](/img/trans.png)
[英]Execution failed due to configuration error: Malformed Lambda proxy response while sending back nested JSON value
[英]Execution failed due to configuration error: Malformed Lambda proxy response in Node JS Lambda
This is Node JS Lambda, I am returning status code 200 as the response, when i call my lambda function through api gateway, i am getting 502 error, i am sending post request to my API Gateway URL. I am using Lambda proxy api integration .
request( options, function ( error, res, body ) {
if ( error ) {
console.log( "this is error", error );
return callback( error );
} else {
// console.dir( body );
const response = {
statusCode: 200,
body: JSON.stringify({
message: 'Your function executed successfully!',
// input: event,
}),
};
return callback( null, {
response,
} );
}
} );
我看到這是日志
(543396a4-952b-451e-8e61-6aeedd2463e9) Endpoint response body before transformations:
{
"response": {
"statusCode": 200,
"body": "{\"message\":\"Your function executed successfully!\"}"
}
}
(543396a4-952b-451e-8e61-6aeedd2463e9) Execution failed due to configuration error:
Malformed Lambda proxy response
(543396a4-952b-451e-8e61-6aeedd2463e9) Method completed with status: 502
這有點不直觀,但響應需要有幾個額外的屬性,並且 json 有效負載需要格式更正確一些。
最終響應將是:
return {
statusCode: 200,
headers: {
"Access-Control-Allow-Origin": "*", // or add a specific site (can only provide one origin, not multiple)
"Content-Type": "application/json" // must include the content type
},
isBase64Encoded: false, // not necessarily required, but good practice
body: JSON.stringify({
"message": "Your function executed successfully!"
})
}
注意:
這里的關鍵要點是 json 響應的主體,因為body
必須正確形成和字符串化。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.