Hua
Hua

Reputation: 93

AWS lambda :Malformed Lambda proxy response

This is a typical issue while Lambda sitting behind API gateway. I've googled almost all the posts but still can't not figure out why it fails.

I've read the instruction on what the response should be look like https://docs.aws.amazon.com/apigateway/latest/developerguide/set-up-lambda-proxy-integrations.html#api-gateway-simple-proxy-for-lambda-output-format
It is looking for

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

Here is the log of my test from API gateway console (I have double checked make sure the name of fields are exactly the same)

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

Can anyone help?

EDIT

I've removed "body" from response and still get the same error. The following is the actual log

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

The actual JSON response catptured in java app & lambda is

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

Upvotes: 0

Views: 549

Answers (1)

Hua
Hua

Reputation: 93

After comparing to Node.js example, seems that it is to do with the handler.

When using a RequestHandler, the response is a String

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

From API gateway, The response is showing as follows, note that the whole response is a String, and API gateway complains

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

Changed that to RequestStreamHandler , the error is gone

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

From API gateway, the response is as follows

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!"

Upvotes: 1

Related Questions