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

AWS API Gateway CLI更新集成请求时$参数丢失问题求助

Fixing Missing $context Variables in API Gateway Integration Template

The issue here is that your shell is interpreting the $context prefixes as shell variables and expanding (or stripping) them before the command even reaches AWS API Gateway. On top of that, the quoting in your original command is misconfigured, which makes the problem worse. Here's how to resolve it:

Solution 1: Escape $ Signs and Fix Shell Quoting

In bash (and most POSIX shells), you need to escape the $ character with a backslash (\$) to prevent the shell from treating it as a variable. You also need to properly escape inner double quotes in your JSON string so the shell doesn't split the command incorrectly.

Use this corrected CLI command:

aws apigateway update-integration --rest-api-id aoi84daah3 --resource-id jh5tps --http-method POST --patch-operations "op='add',path='/requestTemplates/application~1json',value='{\"api_name\" : \"\$context.resourcePath\",\"http-method\" :\"\$context.httpMethod\"}'"

Key changes made:

  • Each $ is escaped with \$ to preserve it in the final template
  • Inner double quotes in the JSON are escaped with \" to keep them part of the value string
  • The entire --patch-operations argument is wrapped in double quotes to maintain the correct structure

Solution 2: Use a File for Patch Operations (Cleaner Approach)

If shell escaping feels too error-prone, define your patch operations in a JSON file and reference it directly. This avoids all shell quoting headaches.

  1. Create a file named patch-operations.json with this content:
[
  {
    "op": "add",
    "path": "/requestTemplates/application~1json",
    "value": "{\"api_name\" : \"$context.resourcePath\",\"http-method\" :\"$context.httpMethod\"}"
  }
]
  1. Run the CLI command using the file:
aws apigateway update-integration --rest-api-id aoi84daah3 --resource-id jh5tps --http-method POST --patch-operations file://patch-operations.json

After running either solution, check your integration's request template—you’ll see the $context variables preserved exactly as you intended.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.28 06:34:31