AWS API Gateway GET-> Lambda 函数映射模板问题

AWS API Gateway GET-> Lambda function mapping template trouble

我有一个链接到 lambda 函数的 GET api 网关调用(当传递有效测试事件时,它在 AWS UI 中正确测试)。我生成了 javascript api(通过 AWS UI),并通过它在 ember js 中进行调用。我已经用我的对象填充了所有三个参数(params、body、additionalParams),该对象定义了我想通过 API 调用传递的参数。这一切似乎都在工作......除了 API 和 lambda.

之间的数据传递

遗憾的是,我无法让任何数据从 api 请求流入我的 lambda 函数。我尝试了以下不同的映射模板(我一直使用 '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
  ]
 }

以及

{
  "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('$')
}

#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
  }

为什么我的数据不通过 API 流入我的函数?建议?

看起来您至少已经接近您的第一个映射模板了。我对其进行了一些修改,使其能够正常工作:

{
  "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
  ]
}

在模板的某些部分中,您似乎在尝试解析请求的 body - 而 GET 可以具有 body 通常服务器应该忽略它。因此,如果您在 GET 上发送数据,则它应该位于查询字符串、headers、and/or cookie 中。当查询字符串格式如下时,上述模板有效:?adultCount=3&juniorCount=4&totalCost=5&registeringUser=bob&registrations=99,88,66,55.