3

I am implementing an aws lambda function coded in java using the aws-lambda-java-events library. For troubleshooting purpose I am for now just trying to respond back with the same request body

public class LambdaFunctionHandler implements RequestHandler<APIGatewayProxyRequestEvent, APIGatewayProxyResponseEvent> {
    @Override
    public APIGatewayProxyResponseEvent handleRequest(APIGatewayProxyRequestEvent input, Context context) {
        return new APIGatewayProxyResponseEvent().withBody(input.getBody());
    }
}

I have defined the above lambda function and an api gateway but I get the input.getBody as null. The output of the lambda function is an empty json object

{}

I tested this through the AWS lambda test console. The execution succeeds and the output is as above. The api gateway integration request is configured to call the above lambda function on a POST method.

Below are my queries

  1. How do I get the aws test event json as part of the APIGatewayProxyRequestEvent getBody() string?
  2. How do I get the request body passed to the API Gateway POST method in the APIGatewayProxyRequestEvent getBody() string variable? Do I need to define a mapping template? If so how should it look like (I made multiple attempts but with no success)

The request body I want to pass to the API gateway POST method through POSTMAN is

{
    "question":"Hello, how are you doing?"
}

Thanks!

2
  • How are you testing it? I guess you are testing it from lambda console as a normal event. Is that right? Commented Jun 2, 2020 at 14:21
  • Yes, I just realized that I need to test it with a gateway proxy event. Commented Jun 4, 2020 at 11:54

1 Answer 1

6

I tried the same. The code I used was

public class LambdaFunctionHandler implements RequestHandler<APIGatewayProxyRequestEvent, APIGatewayProxyResponseEvent> {

    public APIGatewayProxyResponseEvent handleRequest(APIGatewayProxyRequestEvent apiGatewayProxyRequestEvent,
            Context context) {
        return new APIGatewayProxyResponseEvent().withBody(apiGatewayProxyRequestEvent.getBody());
    }
}

I guess you are testing from the AWS console directly. You cannot test an event-triggered lambda directly.

You have to create the test with "Amazon API Gateway Proxy" event as shown below.

enter image description here

And pass the request body as part of the parameters.

enter image description here

With these inputs, I got the expected output.

enter image description here

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

2 Comments

Thanks for driving my attention towards the gateway proxy event. How do I modify the json to specify the custom request body? Do I need to create an api gateway and specify the details of it here?
When you mention custom request body, do you mean that the response needs to exactly the way you have mentioned in the question? If so, you can pass the value for the body property as below. "body": "{'question':'Hello, how are you doing?'}", The output I got for the request was { "body": "{'question':'Hello, how are you doing?'}" } Does this answer your question?

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.