Aws lambda AWS lambda api网关错误“;格式错误的Lambda代理响应”;
我的AWS ApiGateway调用此lambda函数:Aws lambda AWS lambda api网关错误“;格式错误的Lambda代理响应”;,aws-lambda,aws-api-gateway,Aws Lambda,Aws Api Gateway,我的AWS ApiGateway调用此lambda函数: const handler = (event, context) => { const theResponse = { "statusCode": 200, "isBase64Encoded": false, "headers": { "Content-Type": &qu
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(响应)使用返回JSON.stringify(响应)代码>但这也会返回相同的错误
但是,如果我替换则返回响应代码>返回{“statusCode”:200,“body”:“hello,world”}代码>则API执行时不会出现错误
在我看来,这是一个lamda_代理集成问题,但我不明白为什么。谢谢。熊骑士
我想这与您构建响应对象的方式有关:
- 尝试仅字符串化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;
}
JSON.stringify不起作用,但正在更改module.exports={handler,}代码>到exports.handler
已工作。但我不知道为什么。我以为这两个在语法上是相等的?