在 API 网关的映射模板中嵌套对象的正确方法是什么?

问题描述 投票:0回答:1

当尝试设置接收数据的端点

application/x-www-form-urlencoded
并开始执行 Step Function 时,我能想到的最佳映射模板是:

#set($body = {
  "context": {
    "proxy": {
      "source_ip": "$context.identity.sourceIp",
      "http_method": "$context.httpMethod",
      "request_id": "$context.requestId",
      "url": "$context.domainName"
    }
  },
  "payload": {
    #foreach($token in $input.path('$').split('&'))
      #set($keyVal = $token.split('='))
      #set($key = $util.urlDecode($keyVal[0]))
      #set($val = $util.urlDecode($keyVal[1]))
      "$key": "$val"#if($foreach.hasNext),#end
    #end
  }
})
{
  "input": "$util.escapeJavaScript($body)",
  "stateMachineArn": "arn:aws:states:us-east-1:12345:stateMachine:name"
}

根据我的研究,这似乎是正确的方法,但它总是无法转换:

Execution failed due to configuration error: Unable to transform request

我已经玩了几个小时的映射模板,我不确定可能是什么问题。没有调试工具,很难准确指出可能出了什么问题,尽管我确实部署了多次,上面共享了模板的不同部分。

好像有两个bug:

  1. 在变量中循环遍历请求的主体(可以在here中找到类似的代码)。
  2. 试图逃离身体对象。缺少 AWS 的 VTL 文档,我不确定我在这里做错了什么。

如果有任何帮助,我将不胜感激,更具体地说,是帮助我弄清楚究竟是什么破坏了转变。

amazon-web-services aws-api-gateway velocity
1个回答
0
投票

如果有人在网上搜索到这里,我最终通过手动对对象进行字符串化和转义来“修复”它:

{ 
    "input": "{ \"context\": { \"proxy\": {[...]) \"$key\": \"$val\"#if($foreach.hasNext),#end #end } }",
    "stateMachineArn": "${arn}"
}

我确定有更好的方法,但这可以完成工作。

© www.soinside.com 2019 - 2024. All rights reserved.