question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Headers are received in Camel-Case

See original GitHub issue

When running sam local start-api, I’m getting headers in Camel-Case, (see X-Api-Key and X-User-Id), however. the deployed lambda function is receiving the headers in lower-case (x-api-key and x-user-id).

My local code is working as of 2020-03-09 17:00 GMT+8, and I’m trying to understand why the behaviour is different now.

In sam local:

{
  'headers': {
    'X-Api-Key': '<redacted>',
    'X-User-Id': '<redacted>',
    'User-Agent': 'PostmanRuntime/7.22.0',
    'Accept': '*/*',
    'Cache-Control': 'no-cache',
    'Host': 'localhost:3000',
    'Accept-Encoding': 'gzip, deflate, br',
    'Connection': 'keep-alive',
    'X-Forwarded-Proto': 'http',
    'X-Forwarded-Port': '3000'
  },
  'multiValueHeaders': {
    'X-Api-Key': ['<redacted>'],
    'X-User-Id': ['<redacted>'],
    'User-Agent': ['PostmanRuntime/7.22.0'],
    'Accept': ['*/*'],
    'Cache-Control': ['no-cache'],
    'Host': ['localhost:3000'],
    'Accept-Encoding': ['gzip, deflate, br'],
    'Connection': ['keep-alive'],
    'X-Forwarded-Proto': ['http'],
    'X-Forwarded-Port': ['3000']
  }
}

In AWS Lambda:

{
  'headers': {
    'Accept': '*/*',
    'Accept-Encoding': 'gzip, deflate, br',
    'Cache-Control': 'no-cache',
    'Host': '<redacted>',
    'User-Agent': 'PostmanRuntime/7.22.0',
    'X-Amzn-Trace-Id': 'Root=1-5e6739e1-d6161c849faa6e9a369f680c',
    'x-api-key': '<redacted>',
    'X-Forwarded-For': '175.143.101.237',
    'X-Forwarded-Port': '443',
    'X-Forwarded-Proto': 'https',
    'x-user-id': '<redacted>'
  },
  'multiValueHeaders': {
    'Accept': ['*/*'],
    'Accept-Encoding': ['gzip, deflate, br'],
    'Cache-Control': ['no-cache'],
    'Host': ['<host>'],
    'User-Agent': ['PostmanRuntime/7.22.0'],
    'X-Amzn-Trace-Id': ['Root=1-5e6739e1-d6161c849faa6e9a369f680c'],
    'x-api-key': ['<redacted>'],
    'X-Forwarded-For': ['<IP address>'],
    'X-Forwarded-Port': ['443'],
    'X-Forwarded-Proto': ['https'],
    'x-user-id': ['<redacted>']
  }
}

Snippet of template.yaml I’m using.

Resources:
  BasePythonLayer:
    Type: AWS::Serverless::LayerVersion
    Properties:
      LayerName: pythonBaseLayer
      ContentUri: /app/LambdaCodes/LambdaLayers/PythonLayer
      CompatibleRuntimes:
        - python3.7
  VendorFunction:
    Type: AWS::Serverless::Function
    Properties:
      FunctionName: vendor-function
      Handler: vendorFunction.lambda_handler
      Runtime: python3.7
      Timeout: 120
      CodeUri: ./VendorFunction/
      Layers:
        - !Ref BasePythonLayer
      Events:
        Vendor:
          Type: Api
          Properties:
            Path: /vendor
            Method: ANY
  1. OS: Using docker, python:alpine
  2. sam --version: SAM CLI, version 0.40.0

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:7
  • Comments:19 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
sapessicommented, Mar 17, 2021

I’d recommend updating the function code to use a CaseInsensitiveDict object just like requests does. The Lambda PowerTools also handle this for you with the get_header_value method. In the HTTP protocol, headers are meant to be case insensitive. Right now it’s the x-api-key header but in the future the issues could apply to any other header.

0reactions
mastazicommented, Jul 29, 2021

I was wrong, API Gateway doesn’t lower everything, in some cases headers remain capitalized. Thank you @jfuss for explaining this here https://github.com/aws/aws-sam-cli/issues/3083#issuecomment-887632702

I have closed #3117 for that reason.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Headers are received in Camel-Case · Issue #1860 - GitHub
When running sam local start-api, I'm getting headers in Camel-Case, (see X-Api-Key and X-User-Id), however. the deployed lambda function is ...
Read more >
Angular 10 / .Net 4.8 Request Headers Changed to be Camel ...
HTTP headers are case insensitive. Whenever you lookup a header value by Key it needs to be a case insensitive match. Request.
Read more >
Http Response headers going back to the consumer always ...
Http Response headers going back to the consumer always being converted to all lower case and NOT camel case. MuleSoft Runtime auto-converts the...
Read more >
Split CamelCase headers in M (And a fix for the Record of ...
You can split CamelCase headers using the Table.TransformColumnName and the following list generator custom function:.
Read more >
Headers are getting convert into lower case? | Layer7 API ...
I am facing issue when client is sending Headers in CAPITAL letters, those headers are being converted into lower by API Gateway.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found