tillerstarr
tillerstarr

Reputation: 2636

AWS API Gateway GET-> Lambda function mapping template trouble

I've got a GET api gateway call linked to a lambda function (which tests correctly in the AWS UI when passing a valid test event). I've generated a javascript api (via the AWS UI), and am making my call in ember js via it. I've filled all three parameters (params, body, additionalParams) with my object that defines the parameters I want to pass via the API call. It all appears to be working...except for the data pass between API and lambda.

Sadly, I'm unable to get ANY data to flow from the api request into my lambda function. I've tried the following different mapping templates (and i've ALWAYS used the 'Deploy API'):

#set($inputRoot = $input.path('$'))
{
"adultCount" : $inputRoot.adultCount,
"juniorCount" : $inputRoot.juniorCount,
"totalCost" : $inputRoot.totalCost,
"registeringUser" : "$inputRoot.registeringUser",
"registrations" : [
  #foreach($elem in $inputRoot.registrations)
      "$elem" 
  #if($foreach.hasNext),#end
  #end
  ]
 }

as well as

{
  "querystring" : "#foreach($key in $input.params().querystring.keySet())#if($foreach.index > 0)&#end$util.urlEncode($key)=$util.urlEncode($input.params().querystring.get($key))#end",
  "body" : $input.json('$')
}

and

#set($keys = [])
  #foreach($key in $input.params().querystring.keySet())
    #set($success = $keys.add($key))
  #end

  #foreach($key in $input.params().headers.keySet())
    #if(!$keys.contains($key))
      #set($success = $keys.add($key))
    #end
  #end

  #foreach($key in $input.params().path.keySet())
    #if(!$keys.contains($key))
      #set($success = $keys.add($key))
    #end
  #end

  {
    #foreach($key in $keys)
      "$key":          "$util.escapeJavaScript($input.params($key))"#if($foreach.hasNext),#end
    #end
  }

Why doesn't my data flow through the API into my function? Suggestions?

Upvotes: 0

Views: 800

Answers (1)

Ryan
Ryan

Reputation: 5973

It looks like you were close with your first mapping template at least. I modified it a little and was able to get this to work:

{
  "adultCount" : $input.params('adultCount'),
  "juniorCount" : $input.params('juniorCount'),
  "totalCost" : $input.params('totalCost'),
  "registeringUser" : "$input.params('registeringUser')",
  "registrations" : [
    #foreach($elem in $input.params('registrations').split(','))
        "$elem" 
    #if($foreach.hasNext),#end
    #end
  ]
}

In some parts of your templates it looked like you were trying to parse the body of the request - while a GET can have a body typically servers are supposed to ignore it. So if you're sending data on a GET it is expected to be in the querystring, headers, and/or cookies. The above template works when the querystring is formatted like this: ?adultCount=3&juniorCount=4&totalCost=5&registeringUser=bob&registrations=99,88,66,55.

Upvotes: 1

Related Questions