superagent
superagent copied to clipboard
Using attach results in "The head content contains invalid characters"
Hey, I am trying to attach a file to a request.
let stream = fs.createReadStream(f);
r.attach('something', stream);
Gives me a seemingly unhandlable error The header content contains invalid characters
.
Unrelated but I had the same error with set
when i passed it a hash and ended up being forced to call it for each key value pair.
I'm in node, not a browser.
If I pass an absolute path to attach instead:
error TypeError: The header content contains invalid characters
at validateHeader (_http_outgoing.js:494:11)
at ClientRequest.setHeader (_http_outgoing.js:498:3)
at Request._end (C:\Users\Sam\source\rateit-testset-publicapi\node_modules\superagent\lib\node\index.js:1102:11)
at Request.end (C:\Users\Sam\source\rateit-testset-publicapi\node_modules\superagent\lib\node\index.js:858:8)
at _fullfilledPromise.Promise (C:\Users\Sam\source\rateit-testset-publicapi\node_modules\superagent\lib\request-base.js:239:12)
Same result.
That's most likely about non-ASCII characters in HTTP headers.
Does the filename contain any non-ASCII characters by any chance? Alternatively, are you setting any other headers that could contain non-ASCII characters?
The error doesn't occur if I omit the attach so I dont think its due any other headers. The only other header is an Authorization: Bearer r = r.attach(path.basename(f), f);
Where f
is '/Users/Sam/source/rateit-testset-publicapi/staff-tests/test.png'
I still get the error.
Ok this is my fault. I was adding something to Object's prototype and it was affecting things. That said you have code that does for (x in p)
like
for (const i in headers) {
debug('setting FormData header: "%s: %s"', i, headers[i]);
req.setHeader(i, headers[i]);
}
Which could be rewritten as Object.keys(headers).forEach(x => //do things)
which is a bit nicer than doing a hasOwnProperty check.
Should anyone else be doing oddball stuff in their prototypes I was able to make a my property a non-enumerable ftw.
// Object.prototype.getSafe = function (expression) {
// return safe.safeGet(this, expression);
// };
Object.defineProperty(Object.prototype, 'getSafe', {
value: function (expression) {
return safe.safeGet(this, expression);
},
enumerable: false,
writable: false
});