amazon-web-services - POST URL编码形式到Amazon API Gateway

标签 amazon-web-services aws-lambda aws-api-gateway

我正在创建一个Webhook来接收来自第三方服务的通知,它们将数据发送到内容类型为application/x-www-form-urlencoded的POST正文中。

但是它会产生相同的错误:

{"message": "Could not parse request body into json: Unrecognized token \'name\': was expecting \'null\', \'true\', \'false\' or NaN\n at [Source: [B@456fe137; line: 1, column: 6]"}

我可以使用以下curl调用重现该错误:
% curl -v -X POST -d 'name=Ignacio&city=Tehuixtla' https://rl9b6lh8gk.execute-api.us-east-1.amazonaws.com/prod/mandrillListener
*   Trying 54.230.227.63...
* Connected to rl9b6lh8gk.execute-api.us-east-1.amazonaws.com (54.230.227.63) port 443 (#0)
* TLS 1.2 connection using TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256
* Server certificate: *.execute-api.us-east-1.amazonaws.com
* Server certificate: Symantec Class 3 Secure Server CA - G4
* Server certificate: VeriSign Class 3 Public Primary Certification Authority - G5
> POST /prod/mandrillListener HTTP/1.1
> Host: rl9b6lh8gk.execute-api.us-east-1.amazonaws.com
> User-Agent: curl/7.43.0
> Accept: */*
> Content-Length: 27
> Content-Type: application/x-www-form-urlencoded
>
* upload completely sent off: 27 out of 27 bytes
< HTTP/1.1 400 Bad Request
< Content-Type: application/json
< Content-Length: 180
< Connection: keep-alive
< Date: Thu, 28 Jan 2016 12:29:40 GMT
< x-amzn-RequestId: cd4d9232-c5ba-11e5-a158-b9b39f0b0599
< X-Cache: Error from cloudfront
< Via: 1.1 1915b8b49d2fbff532431a79650103eb.cloudfront.net (CloudFront)
< X-Amz-Cf-Id: cxU2_b5DzIw4M_n3hJBFXTu9AVRBL3GpbQqUId9IxgS004DfLYqYmg==
<
* Connection #0 to host rl9b6lh8gk.execute-api.us-east-1.amazonaws.com left intact
{"message": "Could not parse request body into json: Unrecognized token \'name\': was expecting \'null\', \'true\', \'false\' or NaN\n at [Source: [B@d92973b; line: 1, column: 6]"}

如果我用双引号将主体包裹起来,则效果很好:
% curl -v -X POST -d '"name=Ignacio&city=Tehuixtla"' https://rl9b6lh8gk.execute-api.us-east-1.amazonaws.com/prod/mandrillListener
*   Trying 54.230.227.19...
* Connected to rl9b6lh8gk.execute-api.us-east-1.amazonaws.com (54.230.227.19) port 443 (#0)
* TLS 1.2 connection using TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256
* Server certificate: *.execute-api.us-east-1.amazonaws.com
* Server certificate: Symantec Class 3 Secure Server CA - G4
* Server certificate: VeriSign Class 3 Public Primary Certification Authority - G5
> POST /prod/mandrillListener HTTP/1.1
> Host: rl9b6lh8gk.execute-api.us-east-1.amazonaws.com
> User-Agent: curl/7.43.0
> Accept: */*
> Content-Length: 29
> Content-Type: application/x-www-form-urlencoded
>
* upload completely sent off: 29 out of 29 bytes
< HTTP/1.1 200 OK
< Content-Type: application/json
< Content-Length: 6
< Connection: keep-alive
< Date: Thu, 28 Jan 2016 12:33:20 GMT
< x-amzn-RequestId: 50610606-c5bb-11e5-b140-5d837ffe26ed
< X-Cache: Miss from cloudfront
< Via: 1.1 a670cda0e28541e40881b95b60c672b7.cloudfront.net (CloudFront)
< X-Amz-Cf-Id: mCLKL4eOnpUMd15IXQZw0RStJHw9Vdf3ivdCl37dcmno2JFOfxw0Vg==
<
* Connection #0 to host rl9b6lh8gk.execute-api.us-east-1.amazonaws.com left intact
"true"%

兰巴只有一条线:
context.succeed('true');

如何使api网关不将正文视为json?

我尝试了有关模板映射的文档,但都没有成功,甚至尝试将其转换为静态模板,完全没有变量!在所有情况下,错误都会在进入我的代码之前发生。

最佳答案

尝试如下设置映射模板:

{
  "body" : $input.json('$')
}

这会将您的字符串转换为json并传递给lambda。

从亚马逊docs:$input.json(x)函数计算JSONPath表达式,并将结果作为JSON字符串返回。

关于amazon-web-services - POST URL编码形式到Amazon API Gateway,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35061820/

相关文章:

amazon-web-services - 如何将 s3 内容请求重定向到 lambda?

javascript - 在 Alexa Skill 中输入多个单词

amazon-web-services - AWS API 网关 SSL : certificate subject name (*. execute-api.ap-south-1.amazonaws.com) 与目标主机名 'custom-domain' 不匹配

amazon-web-services - 从 lambda 创建了 cloudwatch 事件,但 Cloudwatch 事件中没有任何内容

amazon-web-services - 通过 AWS CLI 将文件上传到 S3 时出现 "Broken pipe"

string - AWS Cloudformation 属性 SubnetIds 的值必须为字符串列表类型

MySQL使用 Lambda 将 RDS 转储到 S3

Apache 重写重定向到 Amazon Linux AMI 测试页

aws-cloudformation - 为什么通过自定义域访问时 API Gateway 有时会卡住返回 "502 Bad Gateway"

aws-cloudformation - Cloudformation API GW Integration响应/响应 header