Jos*_*ver 9 vtl aws-api-gateway
我正在为AWS API Gateway集成响应编写映射模板.我想将一个键/值对添加到我的Lambda函数返回的JSON对象.
我的函数返回一些像这样的JSON:
{
"id": "1234",
"name": "Foo Barstein"
}
Run Code Online (Sandbox Code Playgroud)
我想模板输出这样的东西:
{
"id": "1234",
"name": "Foo Barstein",
"href": "https://example.tld/thingy/1234"
}
Run Code Online (Sandbox Code Playgroud)
我的映射模板如下所示:
#set($thingy = $input.json('$'))
#set($thingy.href = "https://example.tld/thingy/$thingy.id")
$thingy
Run Code Online (Sandbox Code Playgroud)
但是,我的模板输出未修改$thingy,没有href我试图添加.
我已阅读VTL用户指南,但无济于事.
小智 6
这样的事情对我有用:
#set($body = $input.path('$'))
#set($body.href = "https://example.tld/thingy/$body.id")
$input.json('$')
Run Code Online (Sandbox Code Playgroud)
没有简单的方法可以实现这一点,但您可以解决它:
## Mapping template
#set($body = $input.body)
#set($id = $input.json('$.id'))
{
"custom": {
"href" : "https://example.tld/thingy/$id"
},
"body": $body
}
Run Code Online (Sandbox Code Playgroud)
然后合并 AWS.Lambda 中的所有键(如果您使用 Lambda):
## Lambda handler
exports.handler = function(event, context) {
const requestParams = Object.assign({}, event.body, event.custom);
// ... function code
}
Run Code Online (Sandbox Code Playgroud)
并且requestParams会是你想要的。
| 归档时间: |
|
| 查看次数: |
569 次 |
| 最近记录: |