Dhana
Dhana

Reputation: 563

AWS Api Gateway Setting the header value to a default value using http Integration

I am using AWS API Gateway and I want to set my Integration type to http. I have the integrated url as https:// xxxxxx.com which takes a header "apikey". I am not expecting the end user to pass the header rather I want to set the apikey to some constant value.

I see that there is a way to force the user to make him pass the header(by making header required under the Method Request section. However, I want to set it to default.

For example in all the requests which are internally calling the URL inside the API gateway should pass the header value as "12345".

Upvotes: 1

Views: 6406

Answers (2)

Dhana
Dhana

Reputation: 563

Firstly thanks to @KMO for his help. The following is the solution:-

  1. Enable Http Proxy Integration.
  2. Add the headers apikey=xxxx and Accept-Encoding=identity under the same Integration Request -> Http Headers.
  3. Under Settings -> Binary Media Types set the following as separate Binary Media Types '*', */*. I mean as two different lines.This step is needed to resolve the Gzip action while returning the response.
  4. Add the Query parameter country in the URL Query String Parameters section.
  5. In the Integration Request map the country parameter to ctry by adding the value under mapped from as method.request.querystring.country. This will ensure that the query parameter country you passed in the main URL will be fed to the downstream url as parameter ctry.

The advantage of this apporoach is that, even if you override the header apikey, the one set under the Http Headers will take the precedence.

Upvotes: 1

K Mo
K Mo

Reputation: 2155

You can add/remove/override headers with an Integration Request Mapping Template.

In the API Gateway console, chose the relevant api/resourece/method. Go to Integration Request > Mapping Templates and chose your Content-Type (if requests are going to be received without a Content-Type header, set the Content-Type for the mapping template to application/json, which is the default behaviour).

Then in the actual mapping template add the following:

{
    #set($context.requestOverride.header.apikey= "testMe")
}

This will add (or overwrite if it already exists) a header called apikey with the value "testMe" to all http requests downstream.

If you take this route, then you will need to also map over any other headers, path parameters, query parameters or body that you wish to pass through.

You could loop through the headers and query parameters like this.

## First set the header you are adding
#set($context.requestOverride.header.apikey= "testMe")

## Loop through all incoming headers and set them for downstream request
#foreach($param in $input.params().header.keySet())
    #set($context.requestOverride.header[$param]= $input.params().header.get($param))
    #if($foreach.hasNext) #end

    #end 

## Loop through all incoming query parameters and set them for downstream request
#foreach($param in $input.params().querystring.keySet())
    #set($context.requestOverride.querystring[$param]= $input.params().querystring.get($param))
    #if($foreach.hasNext) #end

    #end   

As you need to ensure that the header apikey is set to a default value, you should set the override for apikey before looping through the rest of the headers as only the first override will take effect.

The relevant AWS documentation can be found here.

The other alternative would be to point your API Gateway at a Lambda and make the call from the Lambda instead.

Upvotes: 3

Related Questions