使用请求模块,我在HEAD请求上得到以下错误,指向某个缩短的301个重定向URL:
{ [Error: Parse Error] bytesParsed: 123, code: 'HPE_INVALID_CONTENT_LENGTH' }例如,我在http://cnb.cx/1vtyQyv上得到了这个。非常容易复制(节点v0.10.29,请求v2.36.0):
var request = require('request');
request({ url:'http://cnb.cx/1vtyQyv', method: 'HEAD' }, function(err, res) {
console.log(err, res);
});以下是curl头请求在此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主体上的内容长度实际上是124,可以用curl http://cnb.cx/1vtyQyv | wc -c进行验证。
该错误是从Node.js的核心http解析器(parser.c)中抛出的,但奇怪的是,request能够遵循这301重定向,并在执行GET请求时成功地返回目标页面(http://www.cnbc.com/id/101793181)的内容,没有错误,这意味着该错误是不必要的:
var request = require('request');
request({ url:'http://cnb.cx/1vtyQyv', method: 'GET' }, function(err, res) {
console.log(err, res);
});这是一个使用节点解短器的问题,在找到完整的URL之前,它会重复进行HEAD请求。
发布于 2014-07-02 20:06:29
它适用于普通节点v0.10.29:
var http = require('http');
http.request({
host: 'cnb.cx',
path: '/1vtyQyv',
method: 'HEAD'
}, function(res) {
console.dir(res);
res.resume();
}).end();不过,这个错误是用request v2.36.0再现的。关于这件事,你可能想要提出问题。
UPDATE:错误是用普通节点再现的,问题不是缩短的URL,而是导致问题的重定向URL:
http.request({
host: 'www.cnbc.com',
path: '/id/101793181',
method: 'HEAD'
}, function(res) {
console.dir(res.statusCode);
console.dir(res.headers);
}).end();
// results in:
//
// events.js:72
// throw er; // Unhandled 'error' event
// ^
// Error: Parse Error
// at Socket.socketOnData (http.js:1583:20)
// at TCP.onread (net.js:527:27)更新2:--原来重定向的Content-Length: -1正在返回Content-Length: -1,这导致了错误。curl -I http://www.cnbc.com/id/101793181显示:
HTTP/1.1 200 OK Date: Wed, 02 Jul 2014 22:23:49 GMT Server: Apache Vary: User-Agent Via: 1.1 aicache6 Content-Length: -1 X-Aicache-OS: 10.10.1.25:80 Connection: Keep-Alive Keep-Alive: max=20
https://stackoverflow.com/questions/24538520
复制相似问题