您可以使用 Apache Velocity Engine VTL 修改 AWS API Gateway 中的有效负载吗?

问题描述

我有一个可以将数据发布到的 AWS API 网关 - 数据被放入 Kinesis Data Stream 以传输到各种数据存储。我想捕获用户代理并将其附加到数据中。由于我已经以特定格式收集数据,因此我不想过多地更改格式 - 而不是将数据嵌套到父对象中,我只想在数据上添加一个键/值用户代理的对象。

到目前为止,我的映射模板如下所示:

#set($payload = "$input.json('$')")
#set($payload.UserAgent = "$context.identity.userAgent")
{
   "StreamName": "<stream-name>","Data": "$util.base64Encode($payload)","PartitionKey": "$context.identity.sourceIp","UserAgent": "$context.identity.userAgent"

}

当我使用 AWS API Gateway 测试实用程序对其进行测试时,我放入了一个请求体类似于 { "foo": "bar" },结果显示

Thu Jun 03 20:31:53 UTC 2021 : Endpoint request body after transformations: {
   "StreamName": "<stream-name>","Data": "eyJmb28iOiJiYXIifQ==","PartitionKey": "test-invoke-source-ip","UserAgent": "aws-internal/3 aws-sdk-java/1.11.1014 Linux/5.4.102-52.177.amzn2int.x86_64 OpenJDK_64-Bit_Server_VM/25.292-b10 java/1.8.0_292 vendor/Oracle_Corporation cfg/retry-mode/legacy"

}

"eyJmb28iOiJiYXIifQ==" 解码时为 { "foo": "bar" },因此未设置用户代理。但我知道 "$context.identity.userAgent" 不为空,因为它显示UserAgent 键,它只是没有附加到 $payload 对象。

Apache Velocity Engine VTL docs 表示您应该能够修改对象的属性

#set - Establishes the value of a reference
Format:

# [ { ] set [ } ] ( $ref = [ ",' ] arg [ ",' ] )

Usage:

$ref - The LHS of the assignment must be a variable reference or a property reference.
arg - The RHS of the assignment,arg is parsed (i.e. interpolated) if enclosed in double quotes,and not parsed if enclosed in single quotes. If the RHS evaluates to null,it is not assigned to the LHS.
...
Property reference: #set( $monkey.blame = $whitehouse.Leak )

知道这里发生了什么吗?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)