将键/值对添加到VTL中的对象(用于API网关) [英] Adding a key/value pair to an object in VTL (for API Gateway)

查看:62
本文介绍了将键/值对添加到VTL中的对象(用于API网关)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为AWS API Gateway集成响应编写映射模板.我想在返回Lambda函数的JSON对象中添加键/值对.

I am writing a mapping template for an AWS API Gateway integration response. I would like to add a key/value pair to the JSON object returned my Lambda function.

我的函数返回这样的JSON:

My function returns some JSON like this:

{
  "id": "1234",
  "name": "Foo Barstein"
}

我希望模板输出如下内容:

I would like the template to output something like this:

{
  "id": "1234",
  "name": "Foo Barstein",
  "href": "https://example.tld/thingy/1234"
}

我的映射模板如下:

#set($thingy = $input.json('$'))
#set($thingy.href = "https://example.tld/thingy/$thingy.id")

$thingy

但是,我的模板输出未修改的 $ thingy ,而没有我尝试添加的 href .

However, my template outputs the unmodified $thingy, without the href I have tried to add.

我已经阅读了 VTL用户指南,但是无济于事.

I've read the VTL user guide, but to no avail.

推荐答案

类似的方法对我有用:

#set($body = $input.path('$'))
#set($body.href = "https://example.tld/thingy/$body.id")
$input.json('$')

这篇关于将键/值对添加到VTL中的对象(用于API网关)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆