2

我处理来自不支持 JSON (Mailgun) 的服务的 HTTP POST。如果我为 POST 创建 AWS API GW 并将其传递给 AWS Lambda 函数,则数据必须采用 JSON 格式。除了尝试将 POST 序列化为 JSON(我不希望这样做)之外,有人知道是否是这种情况吗?

4

1 回答 1

2

我在这里找到了解决方案,对我有用。

https://forums.aws.amazon.com/thread.jspa?messageID=673012&tstart=0#673012

以下是来自原始帖子的完整答案。

分步说明如下:

  1. Amazon API Gateway -> 单击“创建 API”。
  2. API 名称 = “myTestAPI”,从 API 克隆 = 不要从现有 API 克隆,描述 = “测试”
  3. 单击“创建 API”。
  4. 单击“创建资源”。
  5. 资源名称 = “myTestInput”,资源路径 = “mytestinput”。
  6. 单击“创建资源”。
  7. 单击“创建方法”。
  8. 根据需要选择“POST”或“GET”并单击勾号。
  9. 集成类型 = “Lambda 函数”,根据需要选择区域,根据需要编写代码以操作/存储表单数据。
  10. 单击“保存”,单击“确定”授予权限。
  11. 单击“集成请求”。
  12. 单击“映射模板”。
  13. 单击“添加映射模板”。
  14. Content-Type 是“application/x-www-form-urlencoded”,然后单击勾号。
  15. 单击“应用程序/x-www-form-urlencoded”。
  16. 单击“输入直通”旁边的铅笔图标。
  17. 选择“映射模板”。
  18. 将以下内容粘贴到模板框中:

--

## 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
}
  1. 单击“映射模板”下拉列表旁边的勾号。
  2. 单击“<- 方法执行”。
  3. 单击“部署 API”。
  4. 部署阶段 = “新阶段”,阶段名称 = “生产”。
  5. 单击“部署”。
于 2015-11-29T06:22:45.210 回答