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.

TypeError: The header content contains invalid characters

See original GitHub issue

Summary

I recently added a method to the Object prototype and a day later spent a couple of hours fighting with uploads that mysteriously stopped working

Simplest Example to Reproduce

the following works fine:

const fs = require('fs');
const request = require('request');

var url = 'http://localhost:8000/';
var formData = { script: fs.createReadStream('t.txt') };
request.post({url, formData}, (err, resp, body) => {
        console.log(err || 'ok');
});

however, this fails:

const fs = require('fs');
const request = require('request');

Object.prototype.XYZ = function() {
    return 0;
}
var url = 'http://localhost:8000/';
var formData = { script: fs.createReadStream('t.txt') };
request.post({url, formData}, (err, resp, body) => {
        console.log(err || 'ok');
});

interestingly, it seems to be the newlines in the body of the method that break the code i.e. defining the method this way does not express the problem:

Object.prototype.XYZ = function() { return 0; }

Expected Behavior

what I would expect happen is that any methods described in the prototype are not considered as headers

Current Behavior

the call fails with the following stack trace:

TypeError: The header content contains invalid characters
    at ClientRequest.setHeader (_http_outgoing.js:374:11)
    at new ClientRequest (_http_client.js:131:14)
    at Object.request (http.js:26:10)
    at Request.start (/Users/ekkis/dev/node_modules/request/request.js:748:32)
    at Request.write (/Users/ekkis/dev/node_modules/request/request.js:1466:10)
    at FormData.ondata (internal/streams/legacy.js:16:26)
    at emitOne (events.js:96:13)
    at FormData.emit (events.js:191:7)
    at FormData.CombinedStream.write (/Users/ekkis/dev/node_modules/combined-stream/lib/combined_stream.js:118:8)
    at FormData.CombinedStream._pipeNext (/Users/ekkis/dev/node_modules/combined-stream/lib/combined_stream.js:106:8)

Possible Solution

I’m guessing somewhere the code is doing a for (k in o) when it should be doing something like Object.keys(o).forEach(function(k) {}) and that needs fixing

Context

I’m unable to post a form to a listening server. I believe it may be related to this issue https://github.com/request/request/issues/2367 but different in its genesis

Your Environment

software version
request 2.27.0
node 7.7.2
npm 4.1.2
Operating System OSX 10.11.6

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:8

github_iconTop GitHub Comments

5reactions
ekkiscommented, May 10, 2017

the problem isn’t spaces. it’s newlines, because they’re meaningful to headers

3reactions
sirajpathancommented, May 10, 2017

I solved this problem by trimming white spaces from input data of headers.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Node.js HTTP - TypeError: The header content contains ...
I had a similar issue where I was generating a jwt token for Authorization and it was inserting newline characters.
Read more >
Invalid character in header content ["authorization"] · Issue #942
So, I most likely had a character issue when I copied the token from the browser console. I remember it worked with another...
Read more >
TypeError: Invalid character in header content ["Authorization ...
this a exercise from Andrew Meads Complete node.js developer course, the problem seems to be on "set("Authorization",.... any idea how to ...
Read more >
TypeError - The Header Content Contains Invalid Characters
I've created this repo to demo an appraoch that can be taken for a short term fix.
Read more >
TypeError: The header content contains invalid characters ...
Hello. I am receiving this error when I am am running JSReport as a standalone server. The exact error I get is as...
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