Skip to content

Why does API Gateway remap request headers

0

I've come a across a behavior that I don't succeed to explain on the API gateway service.

When running a request on a type HTTP api gateway with the headers Content-Type: application/x-www-form-urlencoded and Accept:application/json

The request header sent to the back-end server is then changed to Accept=application/x-www-form-urlencoded without any configuration on my side to change It. I did not find any documentation describing this behavior. When using the integrated test tool in api gateway the behavior is not the same and the headers stay as requested.

Here is the logs on the integrated test tool

Method request headers: {Accept=application/json,...,Content-Type= application/x-www-form-urlencoded}
...
# Accept header is correctly forwarded
Endpoint request headers: {..., Accept=application/json, ...}
...
# The request response has the correct Content-Type
Method response headers: {..., Content-Type=application/json}

When looking at the gateway execution logs a different behavior is observed:

Method request headers: {Accept=application/json, ..., Content-Type=application/x-www-form-urlencoded}
...
# The request header get overridden by some API gateway behavior
Endpoint request headers: {..., Accept=application/x-www-form-urlencoded, ...}
...
# The server reject the response and return a 406 with a Content-Type=application/json
Method response headers: {..., Content-Type=application/json}

Looking at the difference between the integrated test tool and the feature make me think about this behavior being a bug, but I may have missed something

asked a year ago329 views

1 Answer
-1

API Gateway does indeed modify certain headers when processing requests, which explains the behavior you're observing.

When API Gateway handles requests, it processes header names and query parameters in a case-sensitive way. More importantly, API Gateway may drop, remap, or otherwise modify certain headers when sending them to your integration endpoint.

In your specific case, what's happening is that API Gateway is honoring only the first Accept media type when a request contains multiple media types in its Accept header. Additionally, when you're using a Content-Type of application/x-www-form-urlencoded, API Gateway may be applying a default transformation behavior.

The difference you're seeing between the integrated test tool and actual API execution is because the test invocation of a method uses the default content type of application/json and may ignore specifications of other content types.

To control this behavior, you can:

  1. Define mapping templates for specific Content-Types to control how the request is transformed
  2. Configure the integration passthrough behavior to determine how requests without matching templates are handled

There are three options for passthrough behavior:

  • When no template matches the request Content-Type header
  • When there are no templates defined
  • Never

By setting the appropriate passthrough behavior, you can control whether the method request body passes through to the integration request without transformation.
Sources
Amazon API Gateway important notes - Amazon API Gateway
Mapping template transformations for REST APIs in API Gateway - Amazon API Gateway
Method request behavior for payloads without mapping templates for REST APIs in API Gateway - Amazon API Gateway

answered a year ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.