mailgun - 将 HTTP Post 从 AWS API GW 传递到 Lambda

标签 mailgun aws-lambda aws-api-gateway

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

最佳答案

我在这里找到了一个适合我的解决方案。

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

以下内容来自原始帖子,以获得完整的答案。

Step-by-step instructions are as follows:

  1. Amazon API Gateway -> Click "Create API".
  2. API name = "myTestAPI", Clone from API = Do not clone from existing API, Description = "Test"
  3. Click "Create API".
  4. Click "Create Resource".
  5. Resource Name = "myTestInput", Resource Path = "mytestinput".
  6. Click "Create Resource".
  7. Click "Create Method".
  8. Select "POST" or "GET" as required and click the tick.
  9. Integration type = "Lambda function", pick region as appropriate, write code as appropriate to action / store form data.
  10. Click "Save", click "Ok" to grant permission.
  11. Click "Integration Request".
  12. Click "Mapping Templates".
  13. Click "Add mapping template".
  14. Content-Type is "application/x-www-form-urlencoded" and click the tick.
  15. Click "application/x-www-form-urlencoded".
  16. Click the pencil icon next to "Input passthrough".
  17. Select "Mapping template".
  18. 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
}
  1. Click the tick next to the "Mapping template" dropdown.
  2. Click "<- Method Execution".
  3. Click "Deploy API".
  4. Deployment stage = "New stage", Stage name = "production".
  5. Click "Deploy".

关于mailgun - 将 HTTP Post 从 AWS API GW 传递到 Lambda,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33381995/

相关文章:

amazon-web-services - git 版本控制 lambda 函数

Mailgun:有没有办法检索 Webhook 调用放弃的存储消息?

amazon-web-services - 当 AppSync/Aurora 中发生查询/突变时发送推送通知/电子邮件

javascript - 尝试通过 NodeMailer Mailgun 传输发送 PDF 时出现 400 错误

amazon-web-services - 502 "Internal Server Error"部署时使用 API Gateway + Lambda

python - 如何从 SAM 本地中的另一个 lambda 调用 AWS lambda?

amazon-web-services - AWS STS AssumeRole : Are IAM user credentials needed, 与否?

aws-cloudformation - 没有为方法定义集成(服务 : ApiGateway, 状态代码:400)

javascript - 如何在 mailgun 电子邮件模板(Node js)中分配变量?

javascript - Node Mailgun 未附加文件