1
我从不支持JSON(Mailgun)的服务处理HTTP POST。如果我为POST创建AWS API GW并将其传递给AWS Lambda函数,并且数据必须使用JSON,则会显示此信息。除了尝试将POST序列化为JSON(我不想这么做),有人知道这是否是这种情况?将HTTP POST从AWS API GW传递到Lambda
我从不支持JSON(Mailgun)的服务处理HTTP POST。如果我为POST创建AWS API GW并将其传递给AWS Lambda函数,并且数据必须使用JSON,则会显示此信息。除了尝试将POST序列化为JSON(我不想这么做),有人知道这是否是这种情况?将HTTP POST从AWS API GW传递到Lambda
我在这里找到了一个解决方案,适合我。
https://forums.aws.amazon.com/thread.jspa?messageID=673012&tstart=0#673012
以下是从原来的职位为一个完整的答案。
步骤一步的说明如下:
- 亚马逊API网关 - >点击 “创建API”。
- API name =“myTestAPI”,从API克隆=不从现有API克隆,说明=“测试”
- 单击“创建API”。
- 点击“创建资源”。
- 资源名称=“myTestInput”,资源路径=“mytestinput”。
- 点击“创建资源”。
- 点击“创建方法”。
- 根据需要选择“POST”或“GET”,然后单击打勾。
- 集成类型=“Lambda函数”,根据需要选择区域,根据行为/存储表单数据编写适当的代码。
- 点击“保存”,点击“确定”即可授予权限。
- 点击“集成请求”。
- 单击“映射模板”。
- 点击“添加映射模板”。
- Content-Type是“application/x-www-form-urlencoded”并单击勾号。
- 点击“application/x-www-form-urlencoded”。
- 单击“输入直通”旁边的铅笔图标。
- 选择“映射模板”。
- 以下内容粘贴到模板中:
-
## 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”。
- 部署阶段=“新阶段”,阶段名称=“生产”。
- 点击“部署”。
谢谢,这个完美工作,只有一个需要改变:'内容Type'不再' “应用程序/ x-WWW的形式,进行了urlencoded”''不过多部分/形式data'。 –
这似乎不适用于HTML表单中的空字段。 –