例如,如果我们想用

得到-用户?name =鲍勃

or

获取/用户/鲍勃

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

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

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

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


当前回答

我的目标是传递一个类似于:

 protodb?sql=select * from protodb.prototab

到Node.js 12 Lambda函数通过一个URL从API网关。我尝试了其他答案中的一些想法,但真的想以最原生的API网关UI方式做一些事情,所以我想出了这个对我有用的方法(截至2020年12月的API网关UI):

在给定API的API Gateway控制台中,在资源下,选择get方法。然后选择其Integration Request并在页面顶部为lambda函数填写数据。

滚动到底部并打开映射模板部分。如果没有定义模板,则选择“请求正文传递”(推荐)。

点击添加映射模板,创建一个内容类型为application/json的映射模板,然后点击选中标记按钮。

对于该映射模板,在生成模板的下拉列表中选择方法请求传递,它将用AWS传递所有内容的一般方式填充它下面的文本框。

点击保存按钮。

现在当我测试它的时候,我不能让参数通过事件。在Lambda函数下node JS下的sql。事实证明,当API网关将URL sql查询参数发送给Lambda函数时,它对Node.js来说是这样的:

 var insql = event.params.querystring.sql;

所以对我来说,花了一些时间的技巧是使用JSON。通过Stringify来显示完整的事件堆栈,然后通过下面的部分来从查询字符串中提取SQL参数。

基本上,你可以在API网关中使用默认的传递功能,诀窍是当你在Lambda函数中,参数是如何传递的。

其他回答

查询字符串可以直接在lambda中用javascript解析

GET /user?name =鲍勃

 var name = event.queryStringParameters.name;

但是,这并不能解决GET用户/bob的问题。

从2017年9月开始,您不再需要配置映射来访问请求体。

你所需要做的就是勾选,“使用Lambda代理集成”,在集成请求下,在资源下。

然后,您将能够访问查询参数,路径参数和标题等

event['pathParameters']['param1']
event["queryStringParameters"]['queryparam1']
event['requestContext']['identity']['userAgent']
event['requestContext']['identity']['sourceIP']

在阅读了其中几个答案后,我在2018年8月使用了几个答案的组合,通过python 3.6的lambda检索查询字符串参数。

首先,我去API网关->我的API ->资源(在左边)->集成请求。在底部,选择映射模板,然后为内容输入application/json。

接下来,选择Amazon提供的方法请求传递模板,并选择保存和部署API。

然后,lambda event['params']是你访问所有参数的方式。查询字符串:event['params']['querystring']

得到-用户?name =鲍勃

{
    "name": "$input.params().querystring.get('name')"
}

获取/用户/鲍勃

{
    "name": "$input.params('name')"
}

让它工作的步骤是:

在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)