node.js 无法将请求正文解析为 json:意外字符 (\'-\' (code 45)) AWS Lambda + API + Postman
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/36210087/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Could not parse request body into json: Unexpected character (\'-\' (code 45)) AWS Lambda + API + Postman
提问by JamesG
I have been trying for a few days to get a parameter sent from the API Gateway in AWS to a Lambda function and I am having no success.
我已经尝试了几天来获取从 AWS 中的 API 网关发送到 Lambda 函数的参数,但没有成功。
I decided to start from the beginning so I followed their walkthrough (http://docs.aws.amazon.com/apigateway/latest/developerguide/getting-started.html#getting-started-new-lambda)
我决定从头开始,所以我跟着他们的演练(http://docs.aws.amazon.com/apigateway/latest/developerguide/getting-started.html#getting-started-new-lambda)
I have checked this walkthrough twice and I have followed the steps to the letter.
我已经两次检查了这个演练,我已经按照信中的步骤进行了操作。
Problem
问题
When I test the API from Postman or in Swift I am getting the error:
当我从 Postman 或 Swift 测试 API 时,出现错误:
{"message": "Could not parse request body into json: Unexpected character (\'-\' (code 45)) in numeric value: expected digit (0-9) to follow minus sign, for valid numeric value\n at [Source: [B@c036d15; line: 1, column: 3]"}
{"message": "无法将请求正文解析为 json: 数值中的意外字符 (\'-\' (代码 45)):预期数字 (0-9) 跟随减号,对于有效数值\n [来源:[B@c036d15;行:1,列:3]"}
In postman, When I view the result as JSON I just get
在邮递员中,当我将结果视为 JSON 时,我只是得到
Bad String
坏字符串
Lambda Function
Lambda 函数
The function is the basic example from the Walkthrough:
该函数是演练中的基本示例:
console.log('Loading event');
exports.handler = function(event, context) {
var name = (event.name === undefined ? 'No-Name' : event.name);
console.log('"Hello":"' + name + '"');
context.done(null, {"Hello":name}); // SUCCESS with message
};
When Tested from the Lambda Console and with the Test data I get the result:
当从 Lambda 控制台和测试数据进行测试时,我得到了结果:
{
"Hello": "TestUser123"
}
When Tested from the API Gateway Test, The result is also:
从 API Gateway Test 测试时,结果也是:
{
"Hello": "TestUser123"
}
Can anyone see why both test consoles are allowing this work but when tested with POSTMAN or used within a Swift Script it does not work ?
谁能看出为什么两个测试控制台都允许这项工作,但是当用 POSTMAN 测试或在 Swift 脚本中使用时它不起作用?
Edit 1
编辑 1
In postman, I have set the content-type to application/json
在邮递员中,我已将内容类型设置为application/json
The script returns the default:
该脚本返回默认值:
{
"Hello": "user"
}
However, When I add in the parameters nameand TestUser123in POSTMAN, this is when it returns the error.
但是,当我在 POSTMAN 中添加参数name和TestUser123时,这是返回错误的时候。
Update 1
更新 1
Ok, so I changed the mapping template to one that I found on another answer:
好的,所以我将映射模板更改为我在另一个答案中找到的模板:
{ "name": "$input.params('name')" }
Now the result is:
现在的结果是:
{
"Hello": ""
}
Any Ideas why it is not getting the name?
任何想法为什么它没有得到名字?
回答by Amresh Venugopal
I just got stuck with this today.
我今天就被这个问题困住了。
your mapping template is:
您的映射模板是:
{ "name": "$input.params('name')" }
AWS uses AWS Velocity templates, which; even though looks like JSON, is different.
AWS 使用 AWS Velocity 模板,其中;即使看起来像 JSON,也是不同的。
if you use
如果你使用
{ "name": $input.params('name') } // notice no quotes
for the mapping template right at the integration request step, then it should work as expected.
对于在集成请求步骤中的映射模板,它应该按预期工作。
回答by Jeremy Thompson
Read the error message very carefully, it actually tells you the problem. For example, I got
仔细阅读错误消息,它实际上告诉您问题所在。例如,我得到
Could not parse request body into json: Unexpected character (\'\"\' (code 34)): was expecting comma to separateObject entries
无法将请求正文解析为 json: Unexpected character (\'\"\' (code 34)):期望用逗号分隔对象条目
So the problem is that I'm missing a comma. I check my Lambda's Integration Request - Body Mapping Template:
所以问题是我缺少一个逗号。我检查了我的 Lambda 的 集成请求 - 正文映射模板:
{
"age" : $input.json('$.persondata.age'),
"income" : $input.json('$.persondata.income')
"height" : $input.json('$.persondata.height')
}
Can you spot the problem? I am missing a comma after the income line.
你能发现问题吗?我在收入线后缺少一个逗号。
Here is another example.
这是另一个例子。
Could not parse request body into json: Unexpected character (\'}\' (code 125)): expected a value
无法将请求正文解析为 json: Unexpected character (\'}\' (code 125)): expected a value
When I look at the Integration Request - Body Mapping Template:
当我查看集成请求 - 正文映射模板时:
#set($inputRoot = $input.path('$'))
{
"age" : $inputRoot.age,
"height" : $inputRoot.height,
"income" : $inputRootincome
}
Can you spot the problem? I am missing a dot in $inputRootincome.
你能发现问题吗?我在 $inputRootincome 中遗漏了一个点。

