How to send UpperCase headers in HTTP

The standard states that headers are case insensitive.

Ruby and node both build inline headers.

We use an external server program that expects AuthToken headers to be case sensitive using the .NET platform, and it seems that they are not standards compliant. In this case, we need headers.

+6
source share
1 answer

At the time of writing the following file, setHeader was copied from the _http_outgoing node core lib page

 var http = require('http'); http.OutgoingMessage.prototype.setHeader = function(name, value) { if (arguments.length < 2) { throw new Error('`name` and `value` are required for setHeader().'); } if (this._header) { throw new Error('Can\'t set headers after they are sent.'); } // NO LOWER CASE var key = name//.toLowerCase(); this._headers = this._headers || {}; this._headerNames = this._headerNames || {}; this._headers[key] = value; this._headerNames[key] = name; // Since we're re-defining the method, we can't use this part anymore //if (automaticHeaders[key]) { // this._removedHeader[key] = false; //} }; 

Commented part for lowercase

So, if you have this problem. requires http and override this method with the version you are currently using.

Then it should work correctly. You can do a similar operation to override a method in ruby, but it will not be quick and easy

Then it will work:

 require('request') request({url: 'http://myurl.com', headers: {UpperCaseWorks: 'Yay'}}) 

EDIT: here for a newer version of node

 OutgoingMessage.prototype.setHeader = function setHeader(name, value) { if (this._header) { throw new errors.Error('ERR_HTTP_HEADERS_SENT', 'set'); } validateHeader(name, value); if (!this[outHeadersKey]) this[outHeadersKey] = {}; // no more lower case const key = name//.toLowerCase(); this[outHeadersKey][key] = [name, value]; switch (key.length) { case 10: if (key === 'connection') this._removedConnection = false; break; case 14: if (key === 'content-length') this._removedContLen = false; break; case 17: if (key === 'transfer-encoding') this._removedTE = false; break; } }; 

Looks like he calls this local method, which also needs to be defined

 function validateHeader(name, value) { let err; if (typeof name !== 'string' || !name || !checkIsHttpToken(name)) { err = new errors.TypeError('ERR_INVALID_HTTP_TOKEN', 'Header name', name); } else if (value === undefined) { err = new errors.TypeError('ERR_HTTP_INVALID_HEADER_VALUE', value, name); } else if (checkInvalidHeaderChar(value)) { debug('Header "%s" contains invalid characters', name); err = new errors.TypeError('ERR_INVALID_CHAR', 'header content', name); } if (err !== undefined) { Error.captureStackTrace(err, validateHeader); throw err; } } 

And this one

 const { outHeadersKey } = require('internal/http'); 

In any case, check your version of node for overriding

+4
source

All Articles