本文翻译自: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}
}

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

  1. Day 30 - 实作 Amazon API GateWay 整合 AWS Lambda 与 Dynamodb

    Day 30 - 实作 Amazon API GateWay 整合 AWS Lambda 与 Dynamodb Amazon API GateWay 简介 Amazon API Gateway 是由 ...

  2. Day 32 - 透过手机呼叫 Amazon API Gateway 上传图片到 S3

    Day 32 - 透过手机呼叫 Amazon API Gateway 上传图片到 S3 Day 31 - 使用 Amazon API Gateway 上传图片到 S3 演示了如何透过 API Gate ...

  3. Amazon API Gateway使用IP白名单控制后端服务访问

    一图胜千言 目标 我们需要在Amazon API Gateway设置资源策略,用于控制某些IP段能够访问特定后端服务. 前提 假设已经完成了Amazon API Gateway中REST API的接口 ...

  4. Day 31 - 使用 Amazon API Gateway 上传图片到 S3

    Day 31 - 使用 Amazon API Gateway 上传图片到 S3 建立 S3 存储桶 (bucket),关闭封锁所有公开存取权,并设定一个资料夹允许对外公开读取. 建立 IAM 的角色: ...

  5. AWS Lambda 搭配 Amazon API Gateway (HTTP API)

    AWS Lambda 搭配 Amazon API Gateway (HTTP API) AWS Lambda 是一种无伺服器.事件推动的运算服务,而 Amazon API Gateway 可以让开发人 ...

  6. AWS Lambda 搭配 Amazon API Gateway (REST API)

    AWS Lambda 搭配 Amazon API Gateway (REST API) AWS Lambda 是一种无伺服器.事件推动的运算服务,而 Amazon API Gateway 可以让开发人 ...

  7. websockets_如何将WebSockets与AWS API Gateway和Lambda一起使用来构建实时应用程序

    websockets by Janitha Tennakoon 通过詹妮莎·特纳库恩 如何将WebSockets与AWS API Gateway和Lambda一起使用来构建实时应用程序 (How to ...

  8. aws lambda使用_使用Lambda,Api Gateway和CloudFormation在AWS云上使用Java

    aws lambda使用 在上一篇文章中,我们实现了基于Java的aws lambda函数,并使用CloudFront进行了部署. 由于我们已经设置了lambda函数,因此我们将使用AWS API G ...

  9. 使用Lambda,Api Gateway和CloudFormation在AWS云上使用Java

    在上一篇文章中,我们实现了基于Java的aws lambda函数,并使用CloudFront进行了部署. 由于我们已经设置了lambda函数,因此我们将使用AWS API Gateway将其与http ...

最新文章

  1. 数论计算机科学与技术专业就业前景好,【数学】数学专业就业前景:你看不见的“前途似锦”...
  2. 集成显卡+独立显卡实现三屏显示
  3. S3C2443时钟管理
  4. [实验手册]MPLS/×××分解:防止PE-CE的路由环路
  5. 基础学习笔记之opencv(14):随机数发生器绘制文字
  6. Avro, Protocol Buffers 、Thrift的联系与区别
  7. 华为路由器的静态路由之间的等价路由以及浮动路由
  8. 想要专升本你不得不看的全干货_吐血整理_专升本_计算机文化基础(七)
  9. 处理数字音乐文件用计算机软件,处理数字音乐文件用计算机软件商标
  10. spacy包及trained pipelines安装教程
  11. 快手小店后台数据爬取(selenium+openpyxl)
  12. 阿里云服务器学生应该怎么购买教程以及将远程主机配置成服务器教程
  13. 国家开发银行广东省分行
  14. 后端开发工程师需要掌握的内容
  15. mysql忘记初始密码与更改密码
  16. [NiuKe-Exercise15] A.吉姆的运算式
  17. 截屏工具 HyperSnap
  18. ensp模拟http服务器显示视频,eNSP模拟服务器(HTTP)
  19. 上海期货交易所数据备份软件项目前景与范围文档
  20. git pull 无响应_git pull origin master 遇到的问题

热门文章

  1. Android开发之Serializable 和 Parcelable的区别(源代码分享)
  2. Ecplise SVN 配置和使用
  3. ThreadLocal 原理 以及设计思想
  4. 【Java基础】容器
  5. opengl 贴图坐标控制_材质贴图正确打开方式
  6. JavaScript语言基础2
  7. HTTP 各版本特点与区别
  8. NSJSONSerliazition文档翻译和使用
  9. (005)RN开发 js jsx ts tsx的区别
  10. Flutter开发之ListView使用第三方pull_to_refresh加载更多(36)