3

How can I request the used HTTP Method in an AWS Lambda handler in JAVA? There is a argument 'context', but after a look on it, I can't request the used HTTP method.

HTTP-Methods are: GET, POST, PUT

BTW: Here is the answer for javascript: How to get the HTTP method in AWS Lambda?

best regards, lars

4
  • 1
    That answer for JavaScript will work just as well for Java. You have to map the HTTP method to a context field in your API Gateway mapping template, as described in that answer. Commented Jan 11, 2017 at 15:20
  • thanks to you mark! I mapped the 'http-method' context field. But how can i access this information from java? In javascript its no problem to extend objects as described in the other stackoverflow post. Commented Jan 11, 2017 at 15:50
  • I don't really use Java in AWS Lambda, but it looks like you will have to map any custom properties in your API Gateway mapping template to the event object since you can't extend the Context object in Java. Commented Jan 11, 2017 at 15:54
  • okay. But we're using custom objects as our first argument in the handler. But since we just started with developing with AWS lambda...maybe we shoud switch to javascript ;) Commented Jan 11, 2017 at 16:02

2 Answers 2

2

You have several options on how you could receive the httpMethod in Java. One of the easiest would be to rename 'http-method' to 'httpMethod' in your Integration Request in API Gateway and then use the RequestHandler Interface for your Lambda handler which will marshal your JSON directly to a Java Object:

package example;

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

public class Hello implements RequestHandler<PojoRequest, PojoResponse> {

    public PojoResponse handleRequest(PojoRequest request, Context context) {
        System.out.println(String.format("HTTP method is %s.", request.getHttpMethod()));
        return new PojoResponse();
    }
}

Then you can create whatever Pojo you want to be the request, for example:

package example;

public class PojoRequest {
    private String firstName;
    private String lastName;
    private String httpMethod;

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public String getHttpMethod() {
        return httpMethod;
    }

    public void setHttpMethod(String httpMethod) {
        this.httpMethod = httpMethod;
    }
}

See: http://docs.aws.amazon.com/lambda/latest/dg/java-handler-using-predefined-interfaces.html

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

1 Comment

Thanks Dave for this workaround. But I won't http information in my entity objects. Is there no other option?
-1

The official documentation you are looking for is : AWS Lambda function handler in Java

You must be writing a handler class for the lambda function :

// Handler value: example.Handler
public class CustomSOLambdaHandler implements RequestHandler<Map<String,String>, String>{
  @Override
  public String handleRequest(Map<String,String> event, Context context)

Now let us understand the two arguments of the handleRequest function before we can implement this overridden method :

Map<String, String> event:

  • event is a parameter that represents the input data passed to the Lambda function. In AWS Lambda, events typically contain information about the trigger that invoked the function.
  • It is a Map<String, String>, which means it's a mapping of keys (strings) to values (strings). The specific structure of this map depends on the event source that triggered the Lambda function. AWS Lambda can be triggered by various sources like API Gateway, S3, DynamoDB, etc., and the format of the event parameter will vary accordingly.
  • Your Lambda function can extract information from this event to determine what action to take or process.

Context context:

  • The context parameter is an object that provides information about the execution environment of the Lambda function.
  • It contains details such as the function name, function version, AWS request ID, and more.
  • It can also be used to interact with AWS services or to control the behavior of the Lambda function.

Now, let's consider a simple example:

Suppose you have an AWS Lambda function triggered by an API Gateway. When a client makes an HTTP request to your API, the API Gateway passes the request information to your Lambda function in the event parameter. The event parameter might contain details like the HTTP method (GET, POST, etc.), request headers, request body, and more.

Your handleRequest method can then extract information from the event parameter to process the incoming HTTP request. For instance, you might retrieve the request body, parse it as JSON, perform some logic based on the received data, and return a response.

Here's a very simplified example of how you might use this function in an API Gateway-triggered Lambda:

public class MyLambdaHandler implements RequestHandler<Map<String, String>, String> {
    public String handleRequest(Map<String, String> event, Context context) {
        // Extract information from the 'event' parameter
        String httpMethod = event.get("httpMethod");
        String requestBody = event.get("body");
        
        // Perform some processing based on the request data
        String response = "Hello, StackOverflow Lambda!";
        
        // Return a response
        return response;
    }
}

In this example, the handleRequest method processes an HTTP request passed via the event parameter and returns a simple response. However, the actual implementation can be much more complex, depending on your use case and the data contained in the event parameter.

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.