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

AWS API Gateway MockIntegration不识别带版本后缀的Content-Type如何处理

问题根因

AWS API Gateway 匹配请求Content-Type与集成请求模板键时,会自动剥离Content-Type头部后的所有参数(如你场景中的;version=1.6.3),仅使用纯媒体类型部分进行匹配。你之前将请求模板键设置为携带版本参数的完整值,无法匹配到实际请求的纯媒体类型,导致MockIntegration没有可识别的映射规则,最终返回500内部错误。

解决方案

提供两种适配不同场景的方案,按需选择即可:

方案1:仅匹配目标媒体类型(忽略版本参数)

直接将请求模板的键修改为不带版本参数的纯媒体类型application/vnd.nasa.cmr.umm+json,所有携带该媒体类型+任意参数的请求都会命中该模板:

integration=aws_apigateway.MockIntegration(
            request_templates={
                # 去掉版本参数,API Gateway会自动匹配带版本的同媒体类型请求
                "application/vnd.nasa.cmr.umm+json":  json.dumps({"statusCode": 200})},
            integration_responses=[
                {
                    "statusCode": "200",
                    "responseTemplates": {"application/json": json.dumps({})},
                }
            ],
)

方案2:兼容任意Content-Type请求(通用Mock场景)

如果你的测试场景不需要限制请求的Content-Type,直接使用$default作为请求模板的键,所有请求都会命中该规则直接返回200:

integration=aws_apigateway.MockIntegration(
            request_templates={
                "$default": json.dumps({"statusCode": 200})
            },
            integration_responses=[
                {
                    "statusCode": "200",
                    "responseTemplates": {"application/json": json.dumps({})},
                }
            ],
            passthrough_behavior=aws_apigateway.PassthroughBehavior.NEVER
)

两种方案部署后,你再发起携带version参数的请求就可以正常得到200状态码和空响应。


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.05 20:48:02