例如,如果我们想用

得到-用户?name =鲍勃

or

获取/用户/鲍勃

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

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

method.request.path。在方法请求页面中定义了一个名为parameter-name的路径参数。 method.request.querystring。parameter-name用于在方法请求页面中定义的名为parameter-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)

其他回答

如今,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'])

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

接受的答案对我来说很好,但是扩展gimenete的答案,我想要一个通用的模板,我可以用它来传递所有的查询/路径/头参数(目前只是字符串),我得到了下面的模板。我把它贴在这里,以防有人觉得有用:

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

为了将参数传递给lambda函数,您需要在API Gateway请求和lambda函数之间创建映射。映射在所选API Gateway资源的集成请求->映射模板部分中完成。

创建一个application/json类型的映射,然后在右侧编辑(单击铅笔)模板。

映射模板实际上是一个Velocity模板,你可以使用if,循环,当然还可以打印变量。模板中注入了这些变量,你可以分别访问查询字符串参数、请求头等等。使用下面的代码,您可以重新创建整个查询字符串:

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

注意:点击复选符号保存模板。您可以使用资源中的“test”按钮测试更改。但是为了在AWS控制台中测试查询字符串参数,您需要在资源的Method Request部分中定义参数名称。

注意:查看Velocity用户指南以获得关于Velocity模板语言的更多信息。

然后在你的lambda模板中,你可以执行以下操作来解析查询字符串:

var query = require('querystring').parse(event.querystring)
// access parameters with query['foo'] or query.foo

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

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

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

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

我的2美分:很多答案建议激活“使用Lambda代理集成”选项,并从$.event中获取参数。queryStringParameter或$.event. pathparameters。但如果你碰巧激活了访问控制允许起源(又名CORS),请继续阅读。

在撰写本文时,Lambda代理集成和CORS还不能很好地协同工作。我的方法是禁用Lambda代理集成的复选框,并手动为请求和响应提供一个映射模板,如下所示:

为application/json请求模板:

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

请注意,我故意将属性命名为queryStringParameters和pathParameters,以模拟Lambda代理集成将生成的名称。这样,如果有一天我激活Lambda代理集成,我的lambdas将不会中断。

application/json的响应模板:

#set($payload = $util.parseJson($input.json('$')))
#set($context.responseOverride.status = $payload.statusCode)
$payload.body

你如何在你的lambda (python)中读取这些?(假设参数是可选的)

def handler(event, context):
    body = event["queryStringParameters"] or {}
    result = myfunction(**body)
    return {
        "statusCode": code,
        "headers": {
            "content-type": "application/json",
        },
        "body": result
    }