如何使用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
相关产品推荐
相关产品推荐

