无法将请求正文解析为json:意外字符(\'-\'(代码45))AWS Lambda + API + Postman [英] Could not parse request body into json: Unexpected character (\'-\' (code 45)) AWS Lambda + API + Postman
问题描述
我已经尝试了几天,以将参数从AWS中的API网关发送到Lambda函数,但没有成功.
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.
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)
我已经两次检查了本演练,并按照步骤执行了.
I have checked this walkthrough twice and I have followed the steps to the letter.
当我从Postman或Swift中测试API时,出现错误:
When I test the API from Postman or in Swift I am getting the error:
{"message":无法将请求正文解析为json:数字值中的意外字符(\'-\'(代码45)):有效数字值的预期数字(0-9)跟随减号\ n [来源:[B @ c036d15;第1行,第3列]}
{"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]"}
在邮递员中,当我以JSON格式查看结果时,
In postman, When I view the result as JSON I just get
字符串错误
Bad String
Lambda函数
该功能是演练中的基本示例:
Lambda Function
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
};
从Lambda控制台进行测试并使用测试"数据得到结果:
When Tested from the Lambda Console and with the Test data I get the result:
{
"Hello": "TestUser123"
}
在通过API网关测试进行测试时,结果也是:
When Tested from the API Gateway Test, The result is also:
{
"Hello": "TestUser123"
}
谁能看到为什么两个测试控制台都允许这项工作,但是当用POSTMAN进行测试或在Swift脚本中使用时却无法正常工作吗?
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 ?
在邮递员中,我已将内容类型设置为 application/json
In postman, I have set the content-type to application/json
脚本返回默认值:
{
"Hello": "user"
}
但是,当我在POSTMAN中添加参数 name 和 TestUser123 时,这是它返回错误的时间.
However, When I add in the parameters name and TestUser123 in POSTMAN, this is when it returns the error.
好,所以我将映射模板更改为在另一个答案中找到的映射模板:
Ok, so I changed the mapping template to one that I found on another answer:
{ "name": "$input.params('name')" }
现在的结果是:
{
"Hello": ""
}
有什么想法为什么没有得到这个名字?
Any Ideas why it is not getting the name?
推荐答案
我今天刚被困住了.
您的映射模板是:
{ "name": "$input.params('name')" }
AWS使用AWS Velocity模板;即使看起来像JSON,也有所不同.
AWS uses AWS Velocity templates, which; even though looks like JSON, is different.
如果您使用
{ "name": $input.params('name') } // notice no quotes
直接在集成请求步骤中获取映射模板,然后它将按预期工作.
for the mapping template right at the integration request step, then it should work as expected.
这篇关于无法将请求正文解析为json:意外字符(\'-\'(代码45))AWS Lambda + API + Postman的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!