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.

luk2302
  • 55,258
  • 23
  • 97
  • 137
Mohit Gupta
  • 215
  • 5
  • 7

3 Answers3

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;
    }
}
Alex Savitsky
  • 2,306
  • 5
  • 24
  • 30
tgk23
  • 103
  • 2
  • 6
  • 1
    This did not work for me. Do I need to configure something else also? – Sunny Apr 04 '20 at 07:19
  • 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! – G M May 31 '23 at 03:18
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;
    }
}
j3App
  • 1,510
  • 1
  • 17
  • 26
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

Vijayanath Viswanathan
  • 8,027
  • 3
  • 25
  • 43