5

Aws Lambda - How to get query params from Api Gateway in lambda function implemented in Java.

I have following code snippet :

package com.amazonaws.lambda.demo;

import com.amazonaws.services.lambda.runtime.Context;
import com.amazonaws.services.lambda.runtime.RequestHandler;

public class LambdaFunctionHandler implements RequestHandler<String, String> {

    @Override
    public String handleRequest(String input, Context context) {
        context.getLogger().log("Input: " + input);

        // TODO: implement your handler
        return "Hello from " + input;
    }
}

How can i access query params if any in above handleRequest function.

3 Answers 3

7

in pom.xml make sure "2.2.5" or newer version is specified here

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

Your java lambda class:

import com.amazonaws.services.lambda.runtime.events.APIGatewayProxyRequestEvent;
import com.amazonaws.services.lambda.runtime.events.APIGatewayProxyResponseEvent;

public class LambdaFunctionHandler
    implements RequestHandler<APIGatewayProxyRequestEvent, APIGatewayProxyResponseEvent> {
    @Override
    public APIGatewayProxyResponseEvent handleRequest(APIGatewayProxyRequestEvent input,
            Context context) {
        Map<String, String> inputParams = input.getPathParameters();

        APIGatewayProxyResponseEvent responseEvent = new APIGatewayProxyResponseEvent();
        responseEvent.setBody("body text");
        responseEvent.setStatusCode(200);
        return responseEvent;
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

This did not work for me. Do I need to configure something else also?
AWS should use your answer for the official documentation; this is the most-complete 'template' that I've found for Lambdas for Java. It's minimal and gives you all of the types you need to process HTTP requests. Would upvote more than once if I could!
1

Based on tgk23's answer above, I believe you are looking for the query parameters, not the path parameters:

public class LambdaFunctionHandler
        implements RequestHandler<APIGatewayProxyRequestEvent, APIGatewayProxyResponseEvent> {
    @Override
    public APIGatewayProxyResponseEvent handleRequest(APIGatewayProxyRequestEvent input, Context context) {
        Map<String, String> inputParams = input.getQueryStringParameters();
        for (Map.Entry<String, String> entry : inputParams.entrySet()) {
            LOGGER.info("param: " + entry.getKey() + "=" + entry.getValue());
        }

        APIGatewayProxyResponseEvent responseEvent = new APIGatewayProxyResponseEvent();
        responseEvent.setBody("body text");
        responseEvent.setStatusCode(200);
        return responseEvent;
    }
}

Comments

0

You can try mapping everything you want in integration request body mapping template of API Gateway. Once you construct body mapping template then in the context of lambda you will get the excat json you have constructed.

Please find below link, which I have given a solution for similar kind of question,

https://stackoverflow.com/a/46407780/7666972

1 Comment

Thanks Vijayanath , i will try this and come back.

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.