Pablo
Pablo

Reputation: 29519

HTTP header case

I am dealing with server, which is not accepting uncapitalized headers and unfortunately I can't do much with it.

var headers = {};
headers['User-Agent'] = 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36';
headers['Accept'] = 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8';
headers['Connection'] = 'keep-alive';
headers['Cache-Control'] = 'max-age=0';
headers['Upgrade-Insecure-Requests'] = '1';
headers['Accept-Encoding'] = 'gzip, deflate';
headers['Accept-Language'] = 'en-US,en;q=0.9,ru;q=0.8,hy;q=0.7';

request.post({url: 'http://10.10.10.10/login', headers: headers, ...

this in fact sending out the following

User-Agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp
Connection: keep-alive
Cache-Control: max-age=0
Upgrade-Insecure-Requests: 1
Accept-Encoding: gzip, deflate
Accept-Language: en-US,en;q=0.9,ru;q=0.8,hy;q=0.7
DNT: 1
host: 10.10.10.10
cookie: vvv=765936875155218941

cookie and host are lower cased. How can I alter request, to send out capitalized headers?

Upvotes: 6

Views: 2730

Answers (1)

Estus Flask
Estus Flask

Reputation: 222760

This is not Node.js issue but a supposed issue with particular library, request. In fact, not an issue at all because HTTP headers are case-insensitive. request uses caseless package to enforce lower-cased headers, so it's expected that user headers will be lower-case if consistency is required.

These headers may be left as is, as they should be handled correctly by remote server according to the specs.

It may be necessary to specific header case if a request is supposed to mimic real client request. In this case header object can be traversed manually before a request, e.g.:

const normalizeHeaderCase = require("header-case-normalizer");

const req = request.post('...', { headers: ... });

for (const [name, value] of Object.entries(req.headers)) {
    delete req.headers[name];
    req.headers[normalizeHeaderCase(name)] =  value;
}

req.on('response', function(response) {...});

Upvotes: 5

Related Questions