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

如何使用Terraform在AWS API Gateway的method_response中设置Content-Type头?

问题原因

AWS API Gateway 对响应参数的命名有固定格式要求,响应头类参数必须以method.response.header.作为前缀,你直接使用Content-Type作为key不符合规范,所以会触发参数校验错误。

正确配置方式

首先修改aws_api_gateway_method_response资源的配置,调整response_parameters的key格式:

resource "aws_api_gateway_method_response" "response_200" {
  rest_api_id = aws_api_gateway_rest_api.api.id
  resource_id = aws_api_gateway_resource.api_resource.id
  http_method = aws_api_gateway_method.mymethod.http_method
  status_code = "200"

  response_parameters = {
    "method.response.header.Content-Type" = true
  }
}

如果需要指定Content-Type的返回值,还要在对应的aws_api_gateway_integration_response资源中添加映射配置:

  • 固定返回指定值(比如application/json)的写法:
resource "aws_api_gateway_integration_response" "integration_resp_200" {
  rest_api_id = aws_api_gateway_rest_api.api.id
  resource_id = aws_api_gateway_resource.api_resource.id
  http_method = aws_api_gateway_method.mymethod.http_method
  status_code = aws_api_gateway_method_response.response_200.status_code

  response_parameters = {
    "method.response.header.Content-Type" = "'application/json'"
  }
}
  • 透传后端返回的Content-Type值的写法:
response_parameters = {
  "method.response.header.Content-Type" = "integration.response.header.Content-Type"
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.23 21:54:04