例如,如果我们想用

得到-用户?name =鲍勃

or

获取/用户/鲍勃

如何将这两个例子作为参数传递给Lambda函数?

我在文档中看到了一些关于设置“映射from”的内容,但我在API Gateway控制台中找不到该设置。

method.request.path。在方法请求页面中定义了一个名为parameter-name的路径参数。 method.request.querystring。parameter-name用于在方法请求页面中定义的名为parameter-name的查询字符串参数。

尽管我定义了一个查询字符串,但我没有看到这两个选项。


当前回答

参考医生: https://docs.aws.amazon.com/apigateway/latest/developerguide/integrating-api-with-aws-services-lambda.html#api-as-lambda-proxy-expose-get-method-with-path-parameters-to-call-lambda-function

需要修改映射模板

其他回答

我已经使用这个映射模板为Lambda事件提供了Body、Headers、Method、Path和URL查询字符串参数。我写了一篇博客文章详细解释了这个模板:http://kennbrodhagen.net/2015/12/06/how-to-create-a-request-object-for-your-lambda-event-from-api-gateway/

下面是你可以使用的映射模板:

{
  "method": "$context.httpMethod",
  "body" : $input.json('$'),
  "headers": {
    #foreach($param in $input.params().header.keySet())
    "$param": "$util.escapeJavaScript($input.params().header.get($param))" #if($foreach.hasNext),#end

    #end
  },
  "queryParams": {
    #foreach($param in $input.params().querystring.keySet())
    "$param": "$util.escapeJavaScript($input.params().querystring.get($param))" #if($foreach.hasNext),#end

    #end
  },
  "pathParams": {
    #foreach($param in $input.params().path.keySet())
    "$param": "$util.escapeJavaScript($input.params().path.get($param))" #if($foreach.hasNext),#end

    #end
  }  
}

这里的很多答案都很棒。但我想要简单一点的东西。 我想要一些东西,将与“Hello World”样本免费工作。这意味着我想要一个简单的生成一个匹配查询字符串的请求体:

{
#foreach($param in $input.params().querystring.keySet())
  "$param": "$util.escapeJavaScript($input.params().querystring.get($param))" #if($foreach.hasNext),#end
#end
}

我认为上面的答案在构建一些真实的东西时产生了一些更有用的东西,但是对于使用AWS的模板快速运行一个hello世界来说,这非常有用。

让它工作的步骤是:

在API网关控制台中…

Go to Resources -> Integration Request Click on the plus or edit icon next to the templates dropdown (odd I know since the template field is already open and the button here looks greyed out) 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) put this in the input mapping { "name": "$input.params('name')" } click on the check box next to the templates dropdown (I'm assuming this is what finally saves it)

如今,AWS上的API Gateway控制台中包含了一个下拉模板。

对于您的API,单击资源名称…然后

展开“Body Mapping Templates”

输入

application / json

为Content-Type(必须显式地输入),并单击勾

一个新窗口将打开,显示“生成模板”和一个下拉菜单(见图)。

选择

请求传递

然后点击保存

要访问任何变量,只需使用以下语法(这是Python) 例如URL:

https://yourURL.execute-api.us-west-2.amazonaws.com/prod/confirmReg?token=12345&uid=5

可以得到如下变量:

from __future__ import print_function

import boto3
import json

print('Loading function')


def lambda_handler(event, context):
    print(event['params']['querystring']['token'])
    print(event['params']['querystring']['uid'])

因此,不需要显式地命名或映射您想要的每个变量。

为了回答我自己的一个问题,我遇到了这个技巧。

在API Gateway映射模板中,使用下面的代码来给出HTTP客户端发送的完整查询字符串:

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

这样做的好处是,您不必在查询字符串中限制自己使用一组预定义的映射键。现在您可以接受查询字符串中的任何键值对,如果这是您想要的处理方式的话。

注意:上面只列出了VTL模板可用的变量$input.params(x)。内部可能会发生变化,querystring可能不再可用。