我如何传递像 {val: 1}
这样的 JSON 对象到我的 Lambda 函数作为查询参数?
关注 Standardized way to serialize JSON to query string?我对我的 JSON 对象进行 URL 编码并请求以下内容:mysite.com/path?json=%7B%22val%22%3A%201%7D
如 requestTemplates我尝试了以下两个选项
"json": "$input.params().querystring.json"
"json": "$util.parseJson($input.params().querystring.json)"
但我收到此错误消息:
{"message": "Could not parse request body into json: Unexpected character (\'v\' (code 118)): was expecting comma to separate OBJECT entries\n at [Source: [B@37a2970e; line: 1, column: 47]"}
如果我不对查询字符串进行编码,则:
mysite.com/path?json={"val":1}
我收到 400 错误
最佳答案
$util.urlDecode
解码 URL 编码的字符串 您的映射模板应如下所示:
{"json": $util.urlDecode($input.params().querystring.json)}
对于
mysite.com/path?json=%7B%22val%22%3A%201%7D
此映射模板将生成以下 JSON:{
"json": {
"val": 1
}
}
如果您希望查询字符串 JSON 在根级别传递给您的 Lambda 函数,请将其用作映射模板:
$util.urlDecode($input.params().querystring.json)
关于json - 如何将 JSON 字符串作为查询参数传递给 API Gateway,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36748324/