Underscores in headers AWS API Gateway

0

I have created an API Gateway using Sam cli. Within the request, I am sending a header named access_token, which the API Gateway removes from the request. It only allows sending the header without an underscore like this: access-token

This is the request:

curl --location 'https://localhost:3000/api/testEdpoint'
--header 'access_token: theaccesstoken'

The problem is that I cannot access the client's request to change from access_token to access-token. Is there any workaround or option where I can tell the Gateway to accept underscore in the headers and not remove them from the request?

1 Answer
0

You're right, API Gateway by default doesn't support headers with underscores. There are a few workarounds you can explore to handle the access_token header with an underscore in your SAM CLI-created API Gateway.

  1. Custom Authorizer (Limited Functionality): Implement a custom authorizer function in your Lambda integration. This function can access the original request headers before any modifications by API Gateway. However, custom authorizers can only access a limited set of headers, and access_token might not be included.Just check once by giving it a try.

  2. Custom Lambda Integration : This approach involves creating a custom Lambda integration for your API endpoint. The Lambda function will receive the unmodified request from API Gateway, allowing you to access the original header (access_token) with the underscore. Here's how to implement it: **step 1 : **Create a Lambda Function: This function will handle the request logic and access the original header. **step 2 **: Configure API Gateway Integration: Set the integration type to "AWS_PROXY" and choose your Lambda function as the integration target.

Within your Lambda function, you can access the original request headers, including the one with the underscore, using the event.headers dictionary.

profile pictureAWS
akad
answered 13 days ago
profile picture
EXPERT
reviewed 13 days ago
  • I have tried with the Custom Authorizer and with the Custom Lambda Integration (integration type "AWS_PROXY"), but in both approaches, the headers are removed before they reach the Lambda function.

    I need access to the access_token in the Custom Authorizer, but it was removed before it made it to the lambda function. I read that we can use CloudFront to intercept the header before it makes it to the API Gateway and replace the underscores with hyphens, but I want to leave the current request process as is and try to solve it on our own.

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.

Guidelines for Answering Questions