Not able to pass params POST to AWS Lambda from Amazon API Gateway

+1 vote

I have been trying to pass a query string or route parameter to AWS Lambda from Amazon API Gateway.
I mapped the query string params to AWS Lambda using the API Gateway, using POST Values instead of query-string
Like this : 

{
    "values": "$input.params()"
}

It doesn't seem to be working, does not display any actual form of data.
I am posted it using : 

application/x-www-form-urlencoded

I got my response from my lambda function because of it no doubt it is invoking lambda fine but the main problem here is that I don't get see the POST params anywhere. I am not able to figure out how to map them. I dumped and as a return, what I get on Lambda side is here:

{"values":"{path={}, querystring={}, header={Accept=*/*, Accept-Encoding=gzip, deflate, Accept-Language=en-US,en;q=0.8, Cache-Control=no-cache, CloudFront-Forwarded-Proto=https, CloudFront-Is-Desktop-Viewer=true, CloudFront-Is-Mobile-Viewer=false, CloudFront-Is-SmartTV-Viewer=false, CloudFront-Is-Tablet-Viewer=false, CloudFront-Viewer-Country=US, Content-Type=application/x-www-form-urlencoded, Origin=chrome-extension://fhbjgbiflinjbdggehcddcbncdddomop, Postman-Token=7ba28698-8753-fcb1-1f48-66750ce12ade, Via=1.1 6ba5553fa41dafcdc0e74d152f3a7a75.cloudfront.net (CloudFront), X-Amz-Cf-Id=sc8C7dLcW0BHYopztNYrnddC0hXyFdBzHv0O9aWU1gKhd1D_J2HF3w==, X-Forwarded-For=50.196.93.57, 54.239.140.62, X-Forwarded-Port=443, X-Forwarded-Proto=https}}"
Jun 13, 2018 in AWS by Flying geek
• 3,280 points
8,770 views

3 answers to this question.

+1 vote

For this template :

application/x-www-form-urlencoded 

This line below will be work for all the cases:

{
    "data": {
        #foreach( $token in $input.path('$').split('&') )
            #set( $keyVal = $token.split('=') )
            #set( $keyValSize = $keyVal.size() )
            #if( $keyValSize >= 1 )
                #set( $key = $util.urlDecode($keyVal[0]) )
                #if( $keyValSize >= 2 )
                    #set( $val = $util.urlDecode($keyVal[1]) )
                #else
                    #set( $val = '' )
                #end
                "$key": "$val"#if($foreach.hasNext),#end
            #end
        #end
    }
}

This will transform an input of: 

name=Marcus&email=email%40example.com&message=

into

{
    "data": {
                "name": "Marcus",
                "email": "email@example.com",
                "message": ""
    }
}

In Lambda handler one can use it like this as it returns all input data:

module.exports.handler = function(event, context, cb) {
  return cb(null, {
    data: event.data
  });
};
answered Jun 13, 2018 by Cloud gunner
• 4,670 points
0 votes

All you need to do is check, "Use Lambda Proxy integration", under Integration Request, under the resource.

enter image description here

You'll then be able to access query parameters, path parameters and headers like so

event['pathParameters']['param1']
event["queryStringParameters"]['queryparam1']
event['requestContext']['identity']['userAgent']
event['requestContext']['identity']['sourceIP']
answered Aug 22, 2018 by Priyaj
• 58,020 points
0 votes

This also works well

The steps to get this working are:

Within the API Gateway Console ...

  1. go to Resources -> Integration Request
  2. click on the plus or edit icon next to templates dropdown (odd I know since the template field is already open and the button here looks greyed out)
  3. Explicitly type application/json in the content-type field even though it shows a default (if you don't do this it will not save and will not give you an error message)
  4. put this in the input mapping { "name": "$input.params('name')" }

  5. click on the check box next to the templates dropdown (I'm assuming this is what finally saves it)

answered Aug 22, 2018 by Priyaj
• 58,020 points

Related Questions In AWS

0 votes
1 answer

not able to delete a security group for my Amazon VPC in AWS?

Hi@akhtar, When you try to delete that security ...READ MORE

answered Mar 18, 2020 in AWS by MD
• 95,460 points
2,789 views
0 votes
1 answer

Not able to get_item from AWS dynamodb using python?

You are mixing resource and client objects ...READ MORE

answered Feb 24, 2022 in AWS by Korak
• 5,820 points
1,752 views
+1 vote
3 answers
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
+2 votes
3 answers
0 votes
2 answers
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP