如何从Amazon API Gateway将查询字符串或路由参数传递到AWS Lambda

本文翻译自:How to pass a querystring or route parameter to AWS Lambda from Amazon API Gateway

for instance if we want to use 例如,如果我们想使用

GET /user?name=bob

or 要么

GET /user/bob

How would you pass both of these examples as a parameter to the Lambda function? 您如何将这两个示例作为参数传递给Lambda函数?

I saw something about setting a "mapped from" in the documentation, but I can't find that setting in the API Gateway console. 我在文档中看到了有关设置“映射自”的内容,但是在API Gateway控制台中找不到该设置。

  • method.request.path.parameter-name for a path parameter named parameter-name as defined in the Method Request page. 在“方法请求”页面中定义的名为parameter-name的路径参数的method.request.path.parameter-name
  • method.request.querystring.parameter-name for a query string parameter named parameter-name as defined in the Method Request page. 在“方法请求”页面中定义的名为parameter-name的查询字符串参数的method.request.querystring.parameter-name

I don't see either of these options even though I defined a query string. 即使定义了查询字符串,我也看不到这些选项中的任何一个。


#1楼

参考:https://stackoom.com/question/27SLu/如何从Amazon-API-Gateway将查询字符串或路由参数传递到AWS-Lambda


#2楼

The steps to get this working are: 使此工作正常的步骤是:

Within the API Gateway Console ... 在API网关控制台中...

  1. go to Resources -> Integration Request 转到Resources -> Integration Request
  2. click on the plus or edit icon next to templates dropdown (odd I know since the template field is already open and the button here looks greyed out) 单击模板下拉菜单旁边的加号或编辑图标(奇怪,因为模板字段已经打开,此处的按钮显示为灰色)
  3. 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) 即使它显示默认值,也要在content-type字段中明确输入application/json (如果不这样做,它将不会保存,并且不会显示错误消息)
  4. put this in the input mapping { "name": "$input.params('name')" } 将其放入输入映射{ "name": "$input.params('name')" }

  5. click on the check box next to the templates dropdown (I'm assuming this is what finally saves it) 单击模板下拉菜单旁边的复选框(我假设这是最终保存它的内容)


#3楼

As part of trying to answer one of my own questions here , I came across this trick. 由于试图回答我自己的问题,一部分在这里 ,我遇到了这一招。

In the API Gateway mapping template, use the following to give you the complete query string as sent by the HTTP client: 在API网关映射模板中,使用以下命令为您提供HTTP客户端发送的完整查询字符串:

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

The advantage is that you don't have to limit yourself to a set of predefined mapped keys in your query string. 好处是您不必将自己限制为查询字符串中的一组预定义映射键。 Now you can accept any key-value pairs in the query string, if this is how you want to handle. 现在,如果您要使用这种方式,则可以接受查询字符串中的任何键值对。

Note: According to this , only $input.params(x) is listed as a variable made available for the VTL template. 注:根据这个 ,只有$input.params(x)被列为一个变量的VTL模板可用。 It is possible that the internals might change and querystring may no longer be available. 内部结构可能会更改,并且querystring可能不再可用。


#4楼

In order to pass parameters to your lambda function you need to create a mapping between the API Gateway request and your lambda function. 为了将参数传递给lambda函数,您需要在API Gateway请求和lambda函数之间创建映射。 The mapping is done in the Integration Request -> Mapping templates section of the selected API Gateway resource. 映射是在所选API网关资源的“ Integration Request -> Mapping templates部分中完成的。

Create a mapping of type application/json , then on the right you will edit (click the pencil) the template. 创建类型为application/json的映射,然后在右侧编辑(单击铅笔)模板。

A mapping template is actually a Velocity template where you can use ifs, loops and of course print variables on it. 映射模板实际上是一个Velocity模板,您可以在其中使用ifs,loops,当然还可以在其上打印变量。 The template has these variables injected where you can access querystring parameters, request headers, etc. individually. 模板注入了这些变量 ,您可以在其中分别访问查询字符串参数,请求标头等。 With the following code you can re-create the whole querystring: 使用以下代码,您可以重新创建整个查询字符串:

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

Note: click on the check symbol to save the template. 注意:单击对勾符号以保存模板。 You can test your changes with the "test" button in your resource. 您可以使用资源中的“测试”按钮来测试更改。 But in order to test querystring parameters in the AWS console you will need to define the parameter names in the Method Request section of your resource. 但是,为了在AWS控制台中测试querystring参数,您需要在资源的“ Method Request部分中定义参数名称。

Note: check the Velocity User Guide for more information about the Velocity templating language. 注意:有关Velocity模板语言的更多信息,请参见《 Velocity用户指南》

Then in your lambda template you can do the following to get the querystring parsed: 然后,在lambda模板中,您可以执行以下操作以解析查询字符串:

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

#5楼

The accepted answer worked fine for me, but expanding on gimenete's answer, I wanted a generic template I could use to pass through all query/path/header params (just as strings for now), and I came up the following template. 接受的答案对我来说很好用,但是扩展了gimenete的答案,我想要一个通用模板,我可以使用它来传递所有查询/路径/标头参数(现在只是字符串),然后我提出了以下模板。 I'm posting it here in case someone finds it useful: 我将其张贴在这里,以防有人觉得有用:

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

#6楼

I have used this mapping template to provide Body, Headers, Method, Path, and URL Query String Parameters to the Lambda event. 我已使用此映射模板为Lambda事件提供主体,标题,方法,路径和URL查询字符串参数。 I wrote a blog post explaining the template in more detail: http://kennbrodhagen.net/2015/12/06/how-to-create-a-request-object-for-your-lambda-event-from-api-gateway/ 我写了一篇博客文章,更详细地解释了该模板: http : //kennbrodhagen.net/2015/12/06/how-to-create-a-request-object-for-your-lambda-event-from-api-网关/

Here is the Mapping Template you can use: 这是您可以使用的映射模板:

{
  "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
  }  
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值