AWS Lambda:格式错误的Lambda代理响应

时间:2020-03-19 10:17:17

标签: java amazon-web-services aws-lambda aws-api-gateway

当L ambda坐在API网关后面时,这是一个典型的问题。我已经搜索了几乎所有帖子,但仍然不知道为什么失败。

我已经阅读了有关响应应为什么样的说明 https://docs.aws.amazon.com/apigateway/latest/developerguide/set-up-lambda-proxy-integrations.html#api-gateway-simple-proxy-for-lambda-output-format
它正在寻找

{
    "isBase64Encoded": true|false,
    "statusCode": httpStatusCode,
    "headers": { "headerName": "headerValue", ... },
    "multiValueHeaders": { "headerName": ["headerValue", "headerValue2", ...], ... },
    "body": "..."
}

这是我从API网关控制台进行的测试的日志(我已仔细检查以确保字段名称完全相同

Thu Mar 19 10:07:58 UTC 2020 : Endpoint response body before transformations: "{\"statusCode\":200,\"body\":\"RESULTS\",\"isBase64Encoded\":false}"
Thu Mar 19 10:07:58 UTC 2020 : Execution failed due to configuration error: Malformed Lambda proxy response
Thu Mar 19 10:07:58 UTC 2020 : Method completed with status: 502

有人可以帮忙吗?

编辑

我已从响应中删除了“正文” ,但仍然遇到相同的错误。以下是实际的日志

Thu Mar 19 23:29:54 UTC 2020 : Received response. Status: 200, Integration latency: 24 ms
Thu Mar 19 23:29:54 UTC 2020 : Endpoint response headers: {Date=Thu, 19 Mar 2020 23:29:54 GMT, Content-Type=application/json, Content-Length=48, Connection=keep-alive, x-amzn-RequestId=f2c2c752-a5e0-45e4-9ff0-d91826b51c7b, x-amzn-Remapped-Content-Length=0, X-Amz-Executed-Version=$LATEST, X-Amzn-Trace-Id=root=1-5e740072-46cee9045a56e04b8023816d;sampled=0}
Thu Mar 19 23:29:54 UTC 2020 : Endpoint response body before transformations: "{\"statusCode\":200,\"isBase64Encoded\":false}"
Thu Mar 19 23:29:54 UTC 2020 : Execution failed due to configuration error: Malformed Lambda proxy response
Thu Mar 19 23:29:54 UTC 2020 : Method completed with status: 502

在Java应用和lambda中捕获的实际JSON响应

{"statusCode":200,"isBase64Encoded":false}

1 个答案:

答案 0 :(得分:0)

在与Node.js示例进行比较之后,似乎与处理程序有关。

使用 RequestHandler 时,响应为 String

public class MyLambda implements RequestHandler<Object, String> {
    @Override
    public String handleRequest(Object input, Context context) {

在API网关中,响应如下所示,请注意,整个响应是 String ,并且API网关抱怨

Thu Mar 19 23:29:54 UTC 2020 : Endpoint response body before transformations: "{\"statusCode\":200,\"isBase64Encoded\":false}"
Thu Mar 19 23:29:54 UTC 2020 : Execution failed due to configuration error: Malformed Lambda proxy response

将其更改为 RequestStreamHandler ,错误消失了

public class APIgatewayTest implements RequestStreamHandler {
    @Override
    public void handleRequest(InputStream input, OutputStream output, Context context) throws IOException {

从API网关返回的响应如下

Thu Apr 09 09:13:08 UTC 2020 : Endpoint response body before transformations: {"statusCode":200,"body":"\"Hello from Lambda!\""}
Thu Apr 09 09:13:08 UTC 2020 : Method response body after transformations: "Hello from Lambda!"
相关问题