Using the request module I'm getting the following error on HEAD requests to some shortened, 301-redirecting URLs:
{ [Error: Parse Error] bytesParsed: 123, code: 'HPE_INVALID_CONTENT_LENGTH' }
For example, I get this on http://cnb.cx/1vtyQyv. Very easy to reproduce (node v0.10.29, request v2.36.0):
var request = require('request');
request({ url:'http://cnb.cx/1vtyQyv', method: 'HEAD' }, function(err, res) {
console.log(err, res);
});
Here is the result of curl
HEAD request on this URL:
$ curl -I http://cnb.cx/1vtyQyv
HTTP/1.1 301 Moved Permanently
Server: nginx
Date: Wed, 02 Jul 2014 18:16:05 GMT
Content-Type: text/html; charset=utf-8
Connection: keep-alive
Cache-Control: private; max-age=90
Content-Length: 124
Location: http://www.cnbc.com/id/101793181
Mime-Version: 1.0
Set-Cookie: _bit=53b44c65-00194-0369a-281cf10a;domain=.cnb.cx;expires=Mon Dec 29 18:16:05 2014;path=/; HttpOnly
The content length on the body is in fact 124, as can be verified with curl http://cnb.cx/1vtyQyv | wc -c
The error is thrown from within Node.js's core http parser (https://github.com/mattn/http-server/blob/master/http_parser.c), however, strangely, request
is able to follow this 301 redirect and successfully returns contents of the target page (http://www.cnbc.com/id/101793181) with no error when doing a GET request, which suggests that the error isn't necessary:
var request = require('request');
request({ url:'http://cnb.cx/1vtyQyv', method: 'GET' }, function(err, res) {
console.log(err, res);
});
This is an issue using node-unshortener which makes repeated HEAD requests until it finds the full URL.