我正在创建一个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"%
lamba只有一行:
context.succeed('true');
如何让api网关不将身体视为json?
我尝试了关于模板映射的文档没有成功,我甚至尝试将其转换为静态模板,根本没有变量!在所有情况下,错误都会在到达我的代码之前发生。
答案 0 :(得分:11)
尝试将映射模板设置如下:
{
"body" : $input.json('$')
}
这会将你的字符串转换成json并传递给lambda。
来自亚马逊docs:
$input.json(x)
函数计算JSONPath表达式并将结果作为JSON字符串返回。
答案 1 :(得分:3)
这并不是完全相关的,但如果您是Amazon API Gateway的新手,我需要的另一个步骤是在添加映射模板之后(重新)部署您的API建议(如果您以前部署过您的API)。这花了我一大堆调试时间,因为我不明白为什么即使在这里发布建议后我仍然继续得到这个错误。
如果使用AWS控制台,
答案 2 :(得分:1)
使表单数据工作的映射模板非常复杂。这是一个要点:https://gist.github.com/ryanray/668022ad2432e38493df
另外,你可以看到我写的这篇文章有一个如何与Slack集成的例子(他们的钩子将表格数据发送到API网关):http://www.ryanray.me/serverless-slack-integrations
答案 3 :(得分:0)
在 API网关中,为您的资源选择POST方法,选择集成请求并为{{1 }}:
application/x-www-form-urlencoded
或者,您可以简单地传递url编码的字符串:
#set($body = $input.path('$'))
#set($jsonString = $util.urlencode($body))
#set($json = $util.parsejson($jsonString))
{
"body" : $json,
}
然后使用url解码并解析lambda中的JSON。