将HTTP发布从AWS API GW传递到Lambda
我从不支持JSON的服务(Mailgun)处理HTTP POST.如果我创建用于POST的AWS API GW并将其传递给数据必须为JSON的AWS Lambda函数,则会出现.除了尝试将POST序列化为JSON(我不希望这样做)之外,还有谁知道这种情况吗?
I process the HTTP POST from a service that does not support JSON (Mailgun). It appears if I create an AWS API GW for POST and pass this to an AWS Lambda function that the data must be in JSON. Other than trying to serialize the POST to JSON (which I would prefer not to), does anyone know if this is the case?
我在这里找到了一个解决方案,对我有用.
I found a solution here, works for me.
https://forums.aws.amazon. com/thread.jspa?messageID = 673012& tstart = 0#673012
以下是来自原始帖子的完整答案.
The following is from the original post for a complete answer.
分步说明如下:
Step-by-step instructions are as follows:
- Amazon API网关->单击创建API".
- API名称="myTestAPI",从API克隆=不从现有API克隆,描述=测试"
- 点击创建API".
- 点击创建资源".
- 资源名称="myTestInput",资源路径="mytestinput".
- 点击创建资源".
- 点击创建方法".
- 根据需要选择"POST"或"GET",然后单击对勾.
- 集成类型="Lambda函数",根据需要选择区域,根据需要编写代码以操作/存储表单数据.
- 单击保存",单击确定"以授予权限.
- 单击集成请求".
- 点击映射模板".
- 单击添加映射模板".
- Content-Type为"application/x-www-form-urlencoded",然后单击对勾.
- 点击应用程序/x-www-form-urlencoded".
- 单击输入传递"旁边的铅笔图标.
- 选择映射模板".
- 将以下内容粘贴到模板"框中:
- Amazon API Gateway -> Click "Create API".
- API name = "myTestAPI", Clone from API = Do not clone from existing API, Description = "Test"
- Click "Create API".
- Click "Create Resource".
- Resource Name = "myTestInput", Resource Path = "mytestinput".
- Click "Create Resource".
- Click "Create Method".
- Select "POST" or "GET" as required and click the tick.
- Integration type = "Lambda function", pick region as appropriate, write code as appropriate to action / store form data.
- Click "Save", click "Ok" to grant permission.
- Click "Integration Request".
- Click "Mapping Templates".
- Click "Add mapping template".
- Content-Type is "application/x-www-form-urlencoded" and click the tick.
- Click "application/x-www-form-urlencoded".
- Click the pencil icon next to "Input passthrough".
- Select "Mapping template".
- Paste the following into the Template box:
-
## convert HTML POST data or HTTP GET query string to JSON
## get the raw post data from the AWS built-in variable and give it a nicer name
#if ($context.httpMethod == "POST")
#set($rawAPIData = $input.path('$'))
#elseif ($context.httpMethod == "GET")
#set($rawAPIData = $input.params().querystring)
#set($rawAPIData = $rawAPIData.toString())
#set($rawAPIDataLength = $rawAPIData.length() - 1)
#set($rawAPIData = $rawAPIData.substring(1, $rawAPIDataLength))
#set($rawAPIData = $rawAPIData.replace(", ", "&"))
#else
#set($rawAPIData = "")
#end
## first we get the number of "&" in the string, this tells us if there is more than one key value pair
#set($countAmpersands = $rawAPIData.length() - $rawAPIData.replace("&", "").length())
## if there are no "&" at all then we have only one key value pair.
## we append an ampersand to the string so that we can tokenise it the same way as multiple kv pairs.
## the "empty" kv pair to the right of the ampersand will be ignored anyway.
#if ($countAmpersands == 0)
#set($rawPostData = $rawAPIData + "&")
#end
## now we tokenise using the ampersand(s)
#set($tokenisedAmpersand = $rawAPIData.split("&"))
## we set up a variable to hold the valid key value pairs
#set($tokenisedEquals = [])
## now we set up a loop to find the valid key value pairs, which must contain only one "="
#foreach( $kvPair in $tokenisedAmpersand )
#set($countEquals = $kvPair.length() - $kvPair.replace("=", "").length())
#if ($countEquals == 1)
#set($kvTokenised = $kvPair.split("="))
#if ($kvTokenised[0].length() > 0)
## we found a valid key value pair. add it to the list.
#set($devNull = $tokenisedEquals.add($kvPair))
#end
#end
#end
## next we set up our loop inside the output structure "{" and "}"
{
#foreach( $kvPair in $tokenisedEquals )
## finally we output the JSON for this pair and append a comma if this isn't the last pair
#set($kvTokenised = $kvPair.split("="))
"$util.urlDecode($kvTokenised[0])" : #if($kvTokenised[1].length() > 0)"$util.urlDecode($kvTokenised[1])"#{else}""#end#if( $foreach.hasNext ),#end
#end
}
- 单击映射模板"下拉菜单旁边的对勾.
- 单击<-方法执行".
- 点击部署API".
- 部署阶段=新阶段",阶段名称=生产".
- 点击部署".
- Click the tick next to the "Mapping template" dropdown.
- Click "<- Method Execution".
- Click "Deploy API".
- Deployment stage = "New stage", Stage name = "production".
- Click "Deploy".