Learn Hadoop
Learn Hadoop

Reputation: 3050

AWS Lambda function - accessing Path and request parameter value

For my POC, created simple lambda function , which will give emp information through rest api. Created lambda function and access all the emp data using API gateway. Facing some challenges while accessing particular data.

i am looking for

emp/1 - to retrieve emp id emp/_search?name="apple" - search name contains apple.

Question is how to retrieve path and request parameters in java code.

public class TestAwsLambdaFunction implements RequestHandler<Map<String, Object>, String> {

    @Override
    public String handleRequest(Map<String, Object> input, Context context) {
        String empID= null;
        try {
            @SuppressWarnings("unchecked")

            Map<String, String> pathParameters = (Map<String, String>) input.get("querystring");                

            empID= pathParameters.get("id");
            System.out.println(empID);
            // TO-Do Business logic - 


        } catch (Exception e) {
            // TODO: handle exception
        }
        return "Hello from Lambda!" + empID;
    }

}

What is the best way to expose my data in Rest api call. Bit confused with Lambda or serverless . have any option to show the data via page wise. Since i am new to AWS. Please guide me

Upvotes: 0

Views: 2380

Answers (2)

vaquar khan
vaquar khan

Reputation: 11449

Question is how to retrieve path and request parameters in java code.

You can use mapping template to send $input.params('name') property in the request body to your Lambda function.

What is the best way to expose my data in Rest api call

Use the proxy integration with these guidelines:

  • Avoid greedy path variables, except perhaps for a catch-all 404.
  • Avoid using the ANY method.
  • Define request models and enable request validation (remember it’s off by default).
  • In your Lambda, check that the content-type header matches one of your request models, and return a 415 Unsupported Media Type status code if it doesn’t (the proxy integration uses the WHEN_NO_MATCH passthrough behavior). After this check, your Lambda can assume the request validation is fully enforced.

By Ben Kehoe

Upvotes: 1

LiuChang
LiuChang

Reputation: 774

You need to choose Lambda Proxy Integrations when you set up your API Gateway. Here's official document Set up Lambda Proxy Integrations in API Gateway.

In this case, API Gateway will pass the whole request data to Lambda, including the request headers, query string parameters, URL path variables and so on. Then you can parse the data using your Java code.

Upvotes: 1

Related Questions