如何将 header 从请求传递到集成响应?

How to pass header from request to integration response?

对 API 网关的请求将包含某些 header,例如X-header,也必须在响应中返回。因此,这些 headers 必须从请求传递到集成(在本例中是 SQS queue)返回响应(集成响应)。

目前,header 从请求传递到集成;但这不能在集成响应中访问(至少据我所知)。

地形代码:

resource "aws_api_gateway_integration" "integration" {
rest_api_id = gateway.id
resource_id = aws_api_gateway_resource.resource.id
http_method = aws_api_gateway_method.method.http_method
type = "AWS"
integration_http_method = "POST"
uri = "arn:aws:apigateway:XXXX:sqs:path/XXXXX/${var.queue_name}"
credentials = aws_iam_role.iam_role_for_sqs.arn
passthrough_behavior = "NEVER"
request_parameters = {
"integration.request.header.Content-Type" = "'application/x-www-form-urlencoded'",
"integration.request.header.X-header" = "method.request.header.X-header" ,
}
}




  resource "aws_api_gateway_integration_response" "response" {
  rest_api_id = var.gateway.id
  resource_id = aws_api_gateway_resource.resource.id
  http_method = aws_api_gateway_method.method.http_method
  status_code = aws_api_gateway_method_response.response.status_code
  response_parameters = {
        "method.response.header.X-header" = "context.responseOverride.header.X-header", //how to access the header?
  }
}

resource "aws_api_gateway_method_response" "response" {
  rest_api_id = var.gateway.id
  resource_id = aws_api_gateway_resource.resource.id
  http_method = aws_api_gateway_method.method.http_method
  status_code = 200
  response_parameters = { 
        "method.response.header.X-header" = true,
       }
  response_models = {
    "application/json" = "Empty"
  }
}

我可以在文档中找到有关 requestOverride 和 responseOverride 的内容,但无法在集成阶段设置 requestOverride。

如何访问在集成阶段传递给集成响应的请求参数?

省略部分代码。重要的部分是如何访问集成响应中的 header。

已解决: 覆盖响应模板中的 header 将允许您通过 input.params()

访问 header
response_templates = {
    "application/json" = <<EOT
    #set($context.responseOverride.header.X-header = "$util.escapeJavaScript($input.params().header.get('X-header'))"))
        {            
        }
    EOT
}