Dan
Dan

Reputation: 8784

Can't read query parameter in AWS

I am wanting to pass a query parameter from API Gateway into AWS Lambda but I am always receiving null values.

Here's my Lambda function which I merely want to return the value of http://foo.bar?name=Dan

'use strict';

exports.handle = (context, event, callback) => {
  callback(null, event.name);
}

In API Gateway I have done the following:

  1. Create a Resource
  2. Create a Method (GET)
  3. Selected the correct Lambda function
  4. Selected my GET method and clicked on Integration Request
  5. Selected Body Mapping Templates
  6. Set Content-Type to application/json
  7. Added {"name": "$input.params('name')" }
  8. Save and deploy!

However, when I load up my API the value of event.name is always null. Accessing the API is done via ...amazonaws.com/beta/user?name=dan

Edit: I've tried the accepted answer here but after simply returning the event in the callback, I only receive this data:

{
  "callbackWaitsForEmptyEventLoop": true,
  "logGroupName": "",
  "logStreamName": "",
  "functionName": "",
  "memoryLimitInMB": "",
  "functionVersion": "",
  "invokeid": "",
  "awsRequestId": "",
  "invokedFunctionArn": ""
}

I have omitted the values.

Upvotes: 2

Views: 574

Answers (2)

Popoi Menenet
Popoi Menenet

Reputation: 1058

The function arguments' placement for context and event are misplaced. Change their placement as below

'use strict';

exports.handle = (event, context, callback) => {
   callback(null, event.name);
}

Upvotes: 3

Vijayanath Viswanathan
Vijayanath Viswanathan

Reputation: 8541

Even I had the same issue before and I have modified body mapping template like below. Please try it out.

#set($inputRoot = $input.path('$'))
{    
"name" : "$input.params('$.name')"
}

If you are using path parameter then please try below,

#set($inputRoot = $input.path('$'))
{
"name" : "$input.path('$.name')"
}

Upvotes: 2

Related Questions