Jam*_*esG 15 json amazon-web-services node.js aws-lambda
我已经尝试了几天来获取从AWS API网关发送到Lambda函数的参数,但我没有成功.
我决定从头开始,所以我跟着他们的演练(http://docs.aws.amazon.com/apigateway/latest/developerguide/getting-started.html#getting-started-new-lambda)
我已经检查了两次这次演练,我已经按照这些步骤进行了调查.
当我从Postman或Swift测试API时,我收到错误:
{"message":"无法将请求体解析为json:意外字符(\' - \'(代码45))的数值:预期数字(0-9)跟随减号,表示有效数值\n at [来源:[B @ c036d15;第1行,第3列]"}
在postman中,当我将结果视为JSON时,我得到了
坏字符串
该函数是演练中的基本示例:
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
};
Run Code Online (Sandbox Code Playgroud)
从Lambda控制台测试并使用测试数据时,我得到结果:
{
"Hello": "TestUser123"
}
Run Code Online (Sandbox Code Playgroud)
从API网关测试中测试时,结果还是:
{
"Hello": "TestUser123"
}
Run Code Online (Sandbox Code Playgroud)
任何人都可以看到为什么两个测试控制台都允许这项工作,但是当使用POSTMAN进行测试或在Swift脚本中使用它时它不起作用?
在postman中,我已将content-type设置为application/json
该脚本返回默认值:
{
"Hello": "user"
}
Run Code Online (Sandbox Code Playgroud)
但是,当我在POSTMAN中添加参数名称和TestUser123时,这是它返回错误的时间.
好的,所以我将映射模板更改为我在另一个答案中找到的模板:
{ "name": "$input.params('name')" }
Run Code Online (Sandbox Code Playgroud)
结果是:
{
"Hello": ""
}
Run Code Online (Sandbox Code Playgroud)
任何想法为什么它没有得到名字?
我今天刚被困住了。
您的映射模板为:
{ "name": "$input.params('name')" }
Run Code Online (Sandbox Code Playgroud)
AWS使用AWS Velocity模板;即使看起来像JSON,也有所不同。
如果您使用
{ "name": $input.params('name') } // notice no quotes
Run Code Online (Sandbox Code Playgroud)
在集成请求步骤中针对映射模板进行操作,则它应该可以正常工作。
仔细阅读错误消息,它实际上告诉了你问题所在。例如,我得到了
无法将请求正文解析为 json:意外字符(\'\"\'(代码 34)):需要逗号分隔对象条目
所以问题是我少了一个逗号。我检查 Lambda 的 集成请求 - 身体映射模板:
{
"age" : $input.json('$.persondata.age'),
"income" : $input.json('$.persondata.income')
"height" : $input.json('$.persondata.height')
}
Run Code Online (Sandbox Code Playgroud)
你能找出问题所在吗?我在收入行后面少了一个逗号。
这是另一个例子。
无法将请求正文解析为 json:意外字符 (\'}\' (代码 125)):需要一个值
当我查看集成请求 - 正文映射模板时:
#set($inputRoot = $input.path('$'))
{
"age" : $inputRoot.age,
"height" : $inputRoot.height,
"income" : $inputRootincome
}
Run Code Online (Sandbox Code Playgroud)
你能找出问题所在吗?我在 $inputRootvenue 中漏掉了一个点。
小智 5
错误信息 :
Could not parse request body into json: Could not parse payload into json: Unrecognized token \' \': was expecting (\'true\', \'false\' or \'null\')
Run Code Online (Sandbox Code Playgroud)
错误原因:当 json 内的字符串值未在 aws 映射模板中使用双引号分配时,会发生错误。
解决方案:(请注意:此示例适用于 application/json 类型请求模板)
实际上,问题的解决方案是,如果您在 json 中使用字符串类型的值,则应将其值分配在映射模板中的 ("" - 双引号) 内。
下面显示的示例具有以下属性:
Run Code Online (Sandbox Code Playgroud)customerId - string (Please note : this value comes from a query parameter) customerName - string customerAge - integer isPermanentEmployee - boolean customerAddress - string (Please note this is an optional parameter)
并且映射模板应该像下面所示的示例一样定义
请参阅下面的示例:
#set($inputRoot = $input.path('$'))
{
"CustomerId": "$input.params('customerId')",
"CustomerName": "$inputRoot.customerName",
"CustomerAge": $inputRoot.customerAge,
"IsPermanentEmployee": $inputRoot.isPermanentEmployee
"CustomerAddress ": #if($inputRoot.customerAddress == "") "" #elseif($inputRoot.customerAddress != "") "$inputRoot.customerAddress" #end
}
Run Code Online (Sandbox Code Playgroud)
如果您注意到上面的映射模板,我会在双引号(“”)内给出字符串值,这将解决该错误
此示例还包含如何使用 #if#else 语句处理 aws 映射模板中的可选参数。
| 归档时间: |
|
| 查看次数: |
18446 次 |
| 最近记录: |