本文介绍了Node.js HTTP获取URL长度限制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Node.js中发出HTTP GET请求时是否有大小限制?如果是这样,我怎么能改变这个?

Is there a limit to the size when making HTTP GET requests in Node.js? And if so, how can I change this?

var url = "..." // very long, ~50'000 chars
http.get(url, function (res) {
    res.pipe(fs.createWriteStream("file.txt"));
});

给我这个:

<html><body><h1>400 Bad request</h1>
Your browser sent an invalid request.
</body></html>

在PowerShell或Bash中使用wget的功能完全正常。

Same thing using wget in PowerShell or Bash works perfectly fine.

$url = "..."
wget -outf file.txt $url


推荐答案

Node强制执行内置请求大小限制。请求的标头+ URI不应超过80 kb。

There is a built-in request size limit enforced by Node. Requested headers + URI should not be more than 80 kb.

因为它已定义:

/* Maximium header size allowed */
#define HTTP_MAX_HEADER_SIZE (80*1024)

假设UTF-8字符可以介于两者之间1和4字节,具有50 000个字符的字符串的大小将是从50 000到200 000字节,或从~48kb到195kb。

Asuming that UTF-8 character can be between 1 and 4 bytes, the size of a string with 50 000 characters would be from 50 000 to 200 000 bytes, or from ~48kb to 195kb.

这篇关于Node.js HTTP获取URL长度限制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-18 22:37