AWS CLI `apigateway put-method-response` 实战指南:为 API Gateway 方法配置自定义响应头与响应模型 📅 发布时间:2026/9/14 11:15:58 👁 浏览次数: AWS CLIapigateway put-method-response实战指南为 API Gateway 方法配置自定义响应头与响应模型【免费下载链接】aws-cliUniversal Command Line Interface for Amazon Web Services项目地址: https://gitcode.com/GitHub_Trending/aw/aws-cli本篇技术指南以 AWS CLI 官方示例库中的put-method-response命令示例put-method-response.rst为核心系统讲解如何通过 AWS CLI 为 REST API 的某个方法在指定状态码下创建方法响应Method Response并配置自定义响应头。读者学完后将掌握put-method-response的全部参数语义、与put-method/put-integration-response的完整调用链路以及方法响应头与后端集成响应头之间的映射原理能够在实际项目中直接复刻并扩展示例命令。1. 示例命令速览一行命令创建带自定义响应头的方法响应AWS CLI 官方示例库为apigateway put-method-response提供了一个最小化但完整的实战命令见 put-method-response.rstaws apigateway put-method-response \ --rest-api-id 1234123412 \ --resource-id a1b2c3 \ --http-method GET \ --status-code 400 \ --response-parameters method.response.header.custom-headerfalse该命令的作用是在 REST API1234123412的a1b2c3资源下、GET方法的400 状态码上创建一个方法响应并在其中声明一个名为custom-header的自定义响应头值为false表示非必需。示例中的标识均为占位值参数示例值实际来源--rest-api-id1234123412创建 REST API 后返回的 ID如create-rest-api命令的输出--resource-ida1b2c3资源路径对应的资源 ID可用get-resources查询--http-methodGET目标方法使用的 HTTP 动词--status-code400方法响应对应的 HTTP 状态码--response-parametersmethod.response.header.custom-headerfalse方法响应头声明键必须符合method.response.header.{name}模式2. 参数底层定义从服务模型看put-method-response的完整请求结构在 AWS CLI 中每个服务的命令参数均由awscli/botocore/data下的服务模型驱动生成。apigateway的命令模型位于 awscli/botocore/data/apigateway/2015-07-09/service-2.json其中PutMethodResponse操作的定义如下HTTP 方法PUT请求 URI/restapis/{restapi_id}/resources/{resource_id}/methods/{http_method}/responses/{status_code}成功响应码201输入结构PutMethodResponseRequest输出结构MethodResponse可以看到restApiId、resourceId、httpMethod、statusCode四个参数都位于 URI 路径中location: uri是必填参数而responseParameters与responseModels位于请求体均为可选。该操作可能抛出UnauthorizedException、NotFoundException、ConflictException、LimitExceededException、BadRequestException、TooManyRequestsException等异常。2.1 必填参数URI 路径参数参数类型说明--rest-api-idString关联 RestApi 的字符串标识符--resource-idStringMethod 资源所属的 Resource 标识符--http-methodStringMethod 资源的 HTTP 动词--status-codeString方法响应的状态码必须匹配正则[1-5]\d\d即合法的三位 HTTP 状态码2.2 可选参数请求体--response-parametersMapOfStringToBoolean一个键值映射用于指定 API Gateway 可以回传给调用方的必需或可选响应参数。键定义方法响应头名称值为布尔标志表示该响应参数是否必需。关键约束来自 service-2.json 中PutMethodResponseRequest.responseParameters与MethodResponse.responseParameters的文档说明方法响应头名称必须匹配method.response.header.{name}模式其中name是合法且唯一的头部名称这里声明的响应参数可在集成响应Integration Response中被映射映射来源包括集成响应头integration.response.header.{name}静态值用一对单引号包裹例如application/json后端响应负载中的 JSON 表达式integration.response.body.{JSON-expression}不含$前缀。--response-modelsMapOfStringToString指定响应内容类型所使用的 Model 资源。响应模型以键值映射表示键为内容类型content-type值为 Model 名称。2.3 输出结构MethodResponse命令成功后返回MethodResponse结构包含三个字段字段说明statusCode方法响应的状态码responseParameters方法响应头声明映射布尔值表示是否必需responseModels各内容类型对应的响应模型映射如 get-method-response.rst 所示查询已配置的方法响应时会得到类似输出{ responseModels: { application/json: Empty }, statusCode: 200 }3. 完整链路方法、集成响应与方法响应如何协作方法响应Method Response并不是孤立存在的它处于 API Gateway 请求转发链的末端。围绕put-method-responseAWS CLI 示例库提供了完整的配套命令3.1 第一步创建方法put-method在配置方法响应之前目标方法必须已经存在。使用put-method创建方法并声明方法请求头put-method.rstaws apigateway put-method \ --rest-api-id 1234123412 \ --resource-id a1b2c3 \ --http-method PUT \ --authorization-type NONE \ --no-api-key-required \ --request-parameters method.request.header.custom-headerfalse3.2 第二步创建集成响应put-integration-response方法响应声明了对外可见的响应头而实际值来自集成响应。使用put-integration-response定义后端返回数据的映射规则put-integration-response.rst# 默认响应 响应模板 aws apigateway put-integration-response \ --rest-api-id 1234123412 \ --resource-id a1b2c3 \ --http-method GET \ --status-code 200 \ --selection-pattern \ --response-templates {application/json: {\json\: \template\}} # 按正则匹配 400 错误并静态指定响应头值 aws apigateway put-integration-response \ --rest-api-id 1234123412 \ --resource-id a1b2c3 \ --http-method GET \ --status-code 400 \ --selection-pattern 400 \ --response-parameters {method.response.header.custom-header: custom-value}3.3 第三步创建方法响应put-method-response方法响应本例核心命令的作用是把集成响应映射好的数据放行给调用方——只有在方法响应中声明的头method.response.header.*才会最终出现在客户端看到的 HTTP 响应中aws apigateway put-method-response \ --rest-api-id 1234123412 \ --resource-id a1b2c3 \ --http-method GET \ --status-code 400 \ --response-parameters method.response.header.custom-headerfalse三者协作关系可以概括为方法请求头定义入站约束 → 集成响应把后端数据映射到method.response.header.*→ 方法响应决定哪些响应头对外可见。示例中put-integration-response的--response-parameters将custom-header静态映射为custom-value而put-method-response中的false表示该头对调用方而言是可选的并非强制返回。4. 后续管理查询、更新与删除方法响应AWS CLI 示例库还覆盖了方法响应生命周期的其余操作查询get-method-responseaws apigateway get-method-response \ --rest-api-id 1234123412 \ --resource-id y9h6rt \ --http-method GET \ --status-code 200更新update-method-response——通过 PATCH 操作新增响应头值为false表示非必需为默认值或删除某个内容类型的响应模型注意application~1json中~1是 JSON Pointer 对/的转义见 update-method-response.rst# 新增方法响应头非必需 aws apigateway update-method-response \ --rest-api-id 1234123412 \ --resource-id a1b2c3 \ --http-method GET \ --status-code 200 \ --patch-operations opadd,path/responseParameters/method.response.header.custom-header,valuefalse # 删除 application/json 的响应模型 aws apigateway update-method-response \ --rest-api-id 1234123412 \ --resource-id a1b2c3 \ --http-method GET \ --status-code 200 \ --patch-operations opremove,path/responseModels/application~1json删除delete-method-responseaws apigateway delete-method-response \ --rest-api-id 1234123412 \ --resource-id a1b2c3 \ --http-method GET \ --status-code 2005. 实战要点与注意事项状态码合法性--status-code必须匹配[1-5]\d\d服务模型 service-2.json 中StatusCode形状的正则约束例如200、400、500响应头命名约束方法响应头键必须以method.response.header.为前缀名称需合法且唯一不要在方法响应中直接写死具体值具体值应通过集成响应映射false与true的语义response-parameters的布尔值表示该响应头是否为必需required。示例中的false表示可选若改为true则表示 API Gateway 在返回该状态码时必须携带此响应头命令可在集成响应之后或之前执行两者各自独立配置但只有同时存在的头才会被 API Gateway 完整透传配合本地开发验证命令行执行成功后可立即用get-method-response拉取配置核对所有相关命令均可在本地环境直接运行无需修改仓库代码。6. 仓库依据与延伸阅读命令示例原文awscli/examples/apigateway/put-method-response.rst服务模型定义参数结构、正则约束、错误类型awscli/botocore/data/apigateway/2015-07-09/service-2.json配套命令示例awscli/examples/apigateway/put-method.rstawscli/examples/apigateway/put-integration-response.rstawscli/examples/apigateway/get-method-response.rstawscli/examples/apigateway/update-method-response.rstawscli/examples/apigateway/delete-method-response.rst本文中所有参数语义、正则约束与命令用法均以上述仓库文件为依据可直接作为put-method-response命令的查询与开发参考。【免费下载链接】aws-cliUniversal Command Line Interface for Amazon Web Services项目地址: https://gitcode.com/GitHub_Trending/aw/aws-cli创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考