You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

调用API Gateway集成Step Functions时如何过滤响应仅返回output字段?

如何让API Gateway仅返回Step Functions执行结果的output字段内容

已完成API Gateway与Step Functions的集成,但调用网关时返回的响应包含billingDetails、executionArn等多余信息,仅需向客户端返回output字段的内容。当前返回示例如下:

{
  "billingDetails": {
    "billedDurationInMilliseconds": 200,
    "billedMemoryUsedInMB": 64
  },
  "executionArn": "arn:aws:states:us-east-1:0935357777988:express:mystatemachine-stag:d12cb1cd-2094-4690-91f6-31af336e1d0f:42fd1794-c810-46d2-a60d-bb68ed5ad31c",
  "input": "{\"pzId\":\"01GQTJW20W5J99R0ZXD1T3W0Q6\"}",
  "inputDetails": {
    "__type": "com.amazonaws.swf.base.model#CloudWatchEventsExecutionDataDetails",
    "included": true
  },
  "name": "d12cb1cd-2094-4690-91f6-31af336e1d0f",
  "output": "{\"details\":{}",
  "outputDetails": {
    "__type": "com.amazonaws.swf.base.model#CloudWatchEventsExecutionDataDetails",
    "included": true
  },
  "startDate": 1.686226844882E9,
  "stateMachineArn": "arn:aws:states:us-east-1:0935357777988:mystateMachine-stag",
  "status": "SUCCEEDED",
  "stopDate": 1.686226845062E9
}

解决方案:使用API Gateway集成响应映射模板修改输出

通过API Gateway的映射模板可以直接过滤Step Functions返回的响应,仅保留output字段:

  1. 登录API Gateway控制台,定位到目标API及对应的资源方法
  2. 切换到集成响应标签页,找到对应成功状态码(如200)的配置项
  3. 展开映射模板,添加或选择application/json类型的模板
  4. 输入以下VTL代码:
    #set($inputRoot = $input.path('$'))
    $inputRoot.output
    
    如果output字段是JSON字符串(如示例中的格式),需要先解析为JSON对象再返回,避免返回字符串格式:
    #set($inputRoot = $input.path('$'))
    #set($outputJson = $util.parseJson($inputRoot.output))
    $util.toJson($outputJson)
    
  5. 保存配置并重新部署API,再次调用网关即可仅获取output字段的内容

内容的提问来源于stack exchange,提问作者epitka

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.19 22:42:56