1

I am new to aws lambda and api gateway and I am trying to pass a json string to the lambda function.

However, the lambda is not being called with the string.

This is the lambda. It gets an object as input and returns a string representation of the class and the value of the object.


    public class TestMapping implements RequestHandler<Object, String> {
        @Override
        public String handleRequest(Object input, Context context) {
            return "inputClass = " + input.getClass() + ", input = " + String.valueOf(input);
        }
    }

The mapping template for the api gateway is


    #set($allParams = $input.params().querystring)
    {
       "resourcePath" : "$context.resourcePath",
       "httpMethod" : "$context.httpMethod",
       "header": "$input.params().header",
       "path": "$input.params().path", 
       "inputJson": "$input.json()",
       "queryParameters" : [
        #foreach($type in $allParams.keySet())
        {"name": "$type", "value": "$allParams.get($type)"}#if($foreach.hasNext),
        #end
    #end

        ]
    }

The log in the api gateway test says:


    Endpoint request body after transformations: {
       "resourcePath" : "/testmapping",
       "httpMethod" : "GET",
       "header": "{}",
       "path": "{}", 
       "inputJson": "",
       "queryParameters" : [
            {"name": "month", "value": "6"},
            {"name": "employeeId", "value": "1"}
        ]
    }

I expected the json string to be passed to the lambda (as a string object), but instead of this the TestMapping Lambda returns


    "inputClass = class java.util.LinkedHashMap, input = {resourcePath=/testmapping, httpMethod=GET, header={}, path={}, inputJson=, queryParameters=[{name=month, value=6}, {name=employeeId, value=1}]}"

As you can see, a LinkedHashMap is passed to the lambda.

What can I do, to get a string object passed to the lambda with the json string as value?

Deiv
  • 3,000
  • 2
  • 18
  • 30
Sputnik
  • 13
  • 1
  • 3

1 Answers1

0

API Gateway mapping only supports sending a JSON object (which gets converted into a LinkedHashMap when sent to a Java Lambda). You should simply convert the LinkedHashMap into a string inside the Lambda handler.

There is an example of how to do this in this question, which looks like this:

String jsonString = new JSONObject(data).toString()
Deiv
  • 3,000
  • 2
  • 18
  • 30