Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nodejs not retaining upper case of request header

Tags:

node.js

I am using node js as reverse proxy mostly using http and http-proxy module. While sending the request to to nodejs to redirect to one of my application, i have to pass request headers which will all be in upper case. However, nodejs or rather http is converting all upper case to lower case, because of which one of the validation of my application is failing.

My code snippet is:

http.createServer(function (request, response) {

    var redirection = 'http://localhost:8000';
    var path = url.parse(request.url).path;

    switch (path) {
        case '/health':
            proxy.web(request, response, { target: redirection });
            break;
}).listen(8080);

Request headers passed are:

curl -H "X-AUTH: PBxqcEm5sU743Cpk" -X GET http://localhost:8080/health

Now what is happening is, header "X-AUTH" is getting transformed into "x-auth" and my application is not able to validate it. In my application the header matching is case sensitive.

The request headers printed from node js request object are:

{ host: 'localhost:8080',
  'user-agent': 'curl/7.47.1',
  accept: '*/*',
 'x-auth': 'PBxqcEm5sU743Cpk' }

My requirement is to retain the upper case of the header passed in request so that my application can validate and authorize it. Please let me know if there is any way to achieve this

Thanks a lot

like image 544
Shiney.Gaur Avatar asked Apr 27 '17 18:04

Shiney.Gaur


People also ask

Does case matter in HTTP Headers?

All HTTP header keys are converted to lowercase in both directions (since HTTP header keys are defined to be case-insensitive). You should use lowercase in all of your mapping and classification rules.

Are express Headers case sensitive?

header() is case-insensitive so you can use req. header('Content-Type') and req.

Should HTTP Headers be case sensitive?

An HTTP header consists of its case-insensitive name followed by a colon ( : ), then by its value. Whitespace before the value is ignored.

How do I increase the size of the header in node JS?

Node provides us with different ways to change the header size. When you run node server. js , just add the line --max-http-header-size=1024 before server. js and you can change the value 1024 .


1 Answers

FWIW HTTP header field names are case-insensitive so the case really should not matter.

However, node does provide access to the raw headers (including duplicates) via req.rawHeaders. Since req.rawHeaders is an array (format is [name1, value1, name2, value2, ...]), you will need to iterate over it to find the header(s) you are looking for.

like image 145
mscdex Avatar answered Sep 21 '22 04:09

mscdex