1
votes

I'm trying to get the HTTP headers and body details in AWS Lambda function using java, which are sent by POST method using AWS API. So far, I have successfully setup the connection between AWS API and AWS Lambda Function and am able to invoke it from the API Gateway. Now to pass headers to the Lambda function, I have done all the steps before "Updating lambda function" from here. Following is My lambda code:

    import com.amazonaws.Request;
    import com.amazonaws.Response;
    import com.amazonaws.services.lambda.runtime.Context;
    import com.amazonaws.services.lambda.runtime.RequestHandler;
    import com.amazonaws.services.lambda.runtime.events.S3Event;

    public class SimpleSysout implements RequestHandler<Request, String> {
        public String handleRequest(Request request, Context context) {
            System.out.println("Method = "+request.getHttpMethod());
            System.out.println("Headers= "+request.getHeaders().toString());
            System.out.println("Content= "+request.getContent().toString());
            System.out.println("Servcie Name = "+request.getServiceName().toString());
            System.out.println("Resource Path = "+request.getResourcePath().toString());
            return request.toString();
        }

    }

But I get error like

    {
      "errorMessage": "An error occurred during JSON parsing",
      "errorType": "java.lang.RuntimeException",
      "stackTrace": [],
      "cause": {
        "errorMessage": "com.fasterxml.jackson.databind.JsonMappingException: Can not construct instance of com.amazonaws.Request, problem: abstract types either need to be mapped to concrete types, have custom deserializer, or be instantiated with additional type information\n at [Source: lambdainternal.util.NativeMemoryAsInputStream@4dfa3a9d; line: 1, column: 1]",
        "errorType": "java.io.UncheckedIOException",
        "stackTrace": [],
        "cause": {
          "errorMessage": "Can not construct instance of com.amazonaws.Request, problem: abstract types either need to be mapped to concrete types, have custom deserializer, or be instantiated with additional type information\n at [Source: lambdainternal.util.NativeMemoryAsInputStream@4dfa3a9d; line: 1, column: 1]",
          "errorType": "com.fasterxml.jackson.databind.JsonMappingException",
          "stackTrace": [
            "com.fasterxml.jackson.databind.JsonMappingException.from(JsonMappingException.java:148)",
            "com.fasterxml.jackson.databind.DeserializationContext.instantiationException(DeserializationContext.java:889)",
            "com.fasterxml.jackson.databind.deser.AbstractDeserializer.deserialize(AbstractDeserializer.java:139)",
            "com.fasterxml.jackson.databind.ObjectReader._bindAndClose(ObjectReader.java:1511)",
            "com.fasterxml.jackson.databind.ObjectReader.readValue(ObjectReader.java:1102)"
          ]
        }
      }
    }

I have also tried using different parameters instead of the Request, such as events, but none of them work.

2
Can you post your mapping template. - MikeD at AWS

2 Answers

0
votes

Something is wrong with you payload mapping template. During the API gateway setup you can specify how do you want to map the request to the event. There are also a couple of pre-made templates you can select from.

Take a look at this doc from Amazon:

API Gateway API Request and Response Payload-Mapping Template Reference

Let me know if you need more help.

0
votes

handleRequest(InputStream inputStream, OutputStream outputStream,Context context)

I let you a link where is the correct implementation to get your headers from a JSON.

https://www.baeldung.com/aws-lambda-api-gateway