claudiajs / claudia-api-builder

Use AWS API Gateway as if it were a lightweight JavaScript web server

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Documentation: What case do the keys in `request.headers` use?

holyjak opened this issue · comments

The API docs page mentions that headers is a dictionary of headers but does not describe the format of the keys. Are they just as the caller specified them, or are they standardised to a particular case (such as first capital, rest lower case)? Thanks!

They are not standardised in any particular way, just pass the headers the same way you would pass them in http call, ie.

{
  headers: {
    'Content-type': 'application/json'
  }
}

I don't think API gateway specifies this, but from my limited investigation it looks as if they get down cased

If we use standard Node.js request than the headers are lower-cased.

If that is indeed the case, it would be nice to mention it in the documentation of .headers to simplify the life of future devs :-)

It would also mean that https://github.com/claudiajs/claudia-api-builder/blob/master/docs/api.md#the-request-object has misleading code example:

api.corsOrigin(function (request) {
    if (/claudiajs.com$/.test(request.headers.Origin)) {
        return request.headers.Origin; // <--- shouldn't this be lowercase "origin"?!
    }
    return '';
});```

I've done some more testing, and the headers are definitely preserved (so my earlier comment on lowercasing is wrong). I've implemented a change that adds another field .normalizedHeaders, with header names lowercased.

The field is available to APIs deployed with claudia 1.6.0 (so api builder version is not that important). Here are the release notes: https://claudiajs.com/news/2016/08/02/claudia-1.6.0.html

Thank you! This is really useful when one needs to work with the headers.