6

My function is invoked through a proxied API Gateaway. The function executes OK but the API call returns a 502 error:

Mon Dec 30 18:16:25 UTC 2019 : Endpoint response body before transformations: "{}"
Mon Dec 30 18:16:25 UTC 2019 : Execution failed due to configuration error: Malformed Lambda proxy response
Mon Dec 30 18:16:25 UTC 2019 : Method completed with status: 502

The response should be a stringified JSON and should include a statusCode, a body and headers. All these are included below but I still get a Execution failed due to configuration error: Malformed Lambda proxy response. Help?

package example;

import com.amazonaws.services.lambda.runtime.Context;
import com.amazonaws.services.lambda.runtime.RequestHandler;
import javax.json.Json;
import javax.json.JsonObject;
import javax.json.JsonObjectBuilder;
import javax.json.bind.Jsonb;
import javax.json.bind.JsonbBuilder;
import javax.json.bind.JsonbConfig;

/**
 *
 * @author LEVALLOIS
 */
public class Hello implements RequestHandler<Object, String> {

    public static void main(String[] args) {
        String input = "{\"key\":\"value\"}";
        new Hello().handleRequest(input, null);
    }

    @Override
    public String handleRequest(Object input, Context cntxt) {
        System.out.println("in the lambda");
        String data = input != null ? input.toString() : "{}";
        System.out.println("data is: " + data);

        JsonObjectBuilder job1 = Json.createObjectBuilder();
        job1.add("isBase64Encoded", false);
        job1.add("statusCode", 200);

        JsonObjectBuilder job2 = Json.createObjectBuilder();
        JsonObjectBuilder job3 = Json.createObjectBuilder();
        job3.add("key", "value");

        job2.add("Content-Type", "application/json");
        job1.add("headers", job2.build());
        job1.add("body", job3.build().toString());
        System.out.println(job1.build().toString());

        return job1.build().toString();
    }

}

3 Answers 3

8

Try with APIGatewayProxyResponseEvent from lambda-events or create your own class.

<dependency>
    <groupId>com.amazonaws</groupId>
    <artifactId>aws-lambda-java-events</artifactId>
    <version>2.2.9</version>
</dependency>

And inside handler:

public class MyHandler implements RequestHandler<Map<String, Object>, APIGatewayProxyResponseEvent> {

  @Override
  public APIGatewayProxyResponseEvent handleRequest(Map<String, Object> event, Context context) {
    return new APIGatewayProxyResponseEvent()
        .withStatusCode(200)
        .withBody("")
        .withIsBase64Encoded(false);
  }

}
Sign up to request clarification or add additional context in comments.

1 Comment

You are the best!! I was looking for this class for awhile. I'm glad that they provided this library for handling requests. Cheers!
3

Sorry, I can't answer the question. I came here to try to find an answer to more or less the same question. Hopefully the below is helpful...

I believe the output should be a valid JSon String with double-quotes and so on? Like this, which I found in another Stack Overflow thread as a valid example?

{"statusCode":200,"headers":{"Content-Type":"application/json"},"multiValueHeaders":null,"body":"{\"msg\":\"Welcome to Belarus! :)\"}","isBase64Encoded":false}

But when I tried returning this string, it still results in a 502 error. I also tried the above without the double-quoted body structure and the 4 backslashes, which looked wrong to me, but that didn't help. And many other variations of that string, but I always get the 502 error.

I did notice that when I test this from the API Gateway,the output shows

Sat Jan 11 22:53:42 UTC 2020 : Received response. Status: 200, Integration latency: 35 ms
Sat Jan 11 22:53:42 UTC 2020 : Endpoint response headers: {Date=Sat, 11 Jan 2020 22:53:42 GMT, Content-Type=application/json, Content-Length=54, Connection=keep-alive, x-amzn-RequestId=9...}
Sat Jan 11 22:53:42 UTC 2020 : Endpoint response body before transformations: "{\"statusCode\":200,\"body\":\"Welcome to xxx! :)\"}"
Sat Jan 11 22:53:42 UTC 2020 : Execution failed due to configuration error: Malformed Lambda proxy response
Sat Jan 11 22:53:42 UTC 2020 : Method completed with status: 502

i.e. my response message is coming out OK, but then something (the Method Response tab? which I'm pretty sure I never changed, and which the worked examples don't mention) subsequently tries to process the JSon and fails, so changes the statusCode to 502. But I don't know why it's failing.

What does it require the Json to contain?

2 Comments

see my answer above. Not sure that it was the actual issue, but seems likely.
Same problem. @Serg's answer above was the fix for me. No idea why. I was creating a JSON object using GSon and returning the Stringified version, according to repost.aws/knowledge-center/malformed-502-api-gateway. Using the Test feature in the API Gateway to get all my responses, even the failed ones.
0

I forgot to "redeploy" after changes I had made in the API Gateway console. So maybe that I had issues with the json in the response of my lambda, but more probably I did some change in my API Gateway settings, which were not taken into account because I did not re-deploy the API to account for these changes.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.