我的 AWS ApiGateway 调用了这个 lambda 函数:
const handler = (event, context) => {
const theResponse = {
"statusCode": 200,
"isBase64Encoded": false,
"headers": {
"Content-Type": "application/json"
},
"body": "hello, world"
};
switch (event.httpMethod) {
case 'GET':
theResponse["body"] = `method ${event.httpMethod} detected`;
break;
default:
theResponse["statusCode"] = 404;
theResponse["body"] = `Unsupported method ${event.httpMethod} detected`;
break;
}
return theResponse;
};
module.exports = {
handler,
};
关于为什么会出现此错误的任何想法:
{"message": "Internal server error"}
Received response. Status: 200, Integration latency: 344 ms
Endpoint response body before transformations: null
Execution failed due to configuration error: Malformed Lambda proxy response
Method completed with status: 502
我尝试用 return (theResponse);
替换 return JSON.stringify(theResponse);
但这也返回相同的错误。
但是,如果我将 return theResponse;
替换为 return {"statusCode":200, "body":"hello, world"};
,则 API 执行时不会出错。
在我看来像是 lamda_proxy 集成问题,但我不明白为什么。谢谢。
答案 0 :(得分:1)
持枪者!
我想这与您构建响应对象的方式有关:
尝试仅对 body 属性进行字符串化:
case 'GET':
theResponse["body"] = JSON.stringify(`method ${event.httpMethod} detected`);
break;
default:
theResponse["statusCode"] = 404;
theResponse["body"] = JSON.stringify(`Unsupported method ${event.httpMethod} detected`);
break;
}