Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error: The header content contains invalid characters #207

Closed
bookin opened this issue Mar 18, 2017 · 3 comments
Closed

Error: The header content contains invalid characters #207

bookin opened this issue Mar 18, 2017 · 3 comments

Comments

@bookin
Copy link

bookin commented Mar 18, 2017

Code:

let needle = require('needle');

let url = "http://www.business-standard.com/article/opinion/sunil-sethi-ye-gods-what-have-we-done-to-deserve-this…-116092301178_1.html";
let options = {
	compressed:true,
	follow_max: 20,
	user_agent:'Mozilla/5.0 (Windows NT 6.2; rv:22.0) Gecko/20130405 Firefox/23.0',
	open_timeout: 20*1000,
	rejectUnauthorized: true,
	follow_set_cookies: true,
	follow_set_referer: true
}
needle.get(url, options, (err, res) => {
	if(res)
		console.log(res.headers['content-type']);
	if (err){ 
		console.error(err);
	}else{
		console.log(res.body);
	}
});

Falls with an error:

_http_outgoing.js:370
    throw new TypeError('The header content contains invalid characters');
    ^

TypeError: The header content contains invalid characters
    at ClientRequest.setHeader (_http_outgoing.js:370:11)
    at new ClientRequest (_http_client.js:131:14)
    at Object.request (http.js:26:10)
    at Needle.send_request (/home/ubuntu/workspace/node_modules/needle/lib/needle.js:425:26)
    at ClientRequest.<anonymous> (/home/ubuntu/workspace/node_modules/needle/lib/needle.js:467:21)
    at Object.onceWrapper (events.js:291:19)
    at emitOne (events.js:96:13)
    at ClientRequest.emit (events.js:189:7)
    at HTTPParser.parserOnIncomingClient [as onIncoming] (_http_client.js:522:21)
    at HTTPParser.parserOnHeadersComplete (_http_common.js:99:23)

If remove option - follow_set_referer: true, it is works.
How I can fix it?

@tomas
Copy link
Owner

tomas commented Mar 18, 2017

I think it's because the URI contains a "…" char. Try doing let url = encodeURI(string); and see if it works.

@bookin
Copy link
Author

bookin commented Mar 19, 2017

Yes, it is help me, thanks. But strange, without follow_set_referer it is works without encodeURI;

@tomas
Copy link
Owner

tomas commented May 18, 2017

Just added a quick fix to handle these types of scenarios. Thanks for reporting!

@tomas tomas closed this as completed May 18, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants