Hoaz
Hoaz

Reputation: 57

How to pass header from request to integration response?

The requests to the API gateway will contain certain headers, e.g. X-header, that must also be returned in the response. Therefore, these headers must be passed from the request to the integration (which in this case is an SQS queue) back in the response (the integration response).

At the moment, the header is passed from the request to the integration; but this cannot be accessed in the integration response ( to my knowledge, at least).

Terraform code:

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"
  }
}

I could find something in the docs about requestOverride and responseOverride, but it is not possible to set the requestOverride in the integration stage.

How would I access the request parameter passed in the integration stage to the integration response?

Some parts of the code are omitted. The important part is how one would access the headers in the integration response.

Upvotes: 0

Views: 2100

Answers (1)

Hoaz
Hoaz

Reputation: 57

Solved it: Overriding the header in the response template will allow you to access the header via input.params()

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

Upvotes: 2

Related Questions