TypeError: The header content contains invalid characters
See original GitHub issueSummary
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:
- Created 6 years ago
- Comments:8
Top GitHub Comments
the problem isn’t spaces. it’s newlines, because they’re meaningful to headers
I solved this problem by trimming white spaces from input data of headers.