javascript Node.js HTTP 获取 URL 长度限制

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32763165/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 15:44:11  来源:igfitidea点击:

Node.js HTTP Get URL Length limitation

javascriptnode.jshttp

提问by Bruno Sch?pper

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

在 Node.js 中发出 HTTP GET 请求时,大小有限制吗?如果是这样,我该如何改变?

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

Gives me this:

给我这个:

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

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

同样的事情在 PowerShell 或 Bash 中使用 wget 工作得很好。

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

回答by Alexandr Lazarev

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

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

As it is defined in http_parser.h#L55:

正如它在http_parser.h#L55 中定义的那样

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

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.

假设 UTF-8 字符可以在 1 到 4 个字节之间,那么包含 50 000 个字符的字符串的大小将是 50 000 到 200 000 字节,或约 48kb 到 195kb。

回答by Andrew Knackstedt

UPDATE

更新

In newer versions of NodeJS (v12.6.0+), the header request size limit is 8kb by default, and this also impacts the response header size. To use header sizes above 8KB, you can use the switch --max-http-header-size 65535(or whatever size you need). This is described in-depth here.

在较新版本的 NodeJS (v12.6.0+) 中,标头请求大小限制默认为 8kb,这也会影响响应标头大小。要使用超过 8KB 的标头大小,您可以使用开关--max-http-header-size 65535(或您需要的任何大小)。这在此处进行了深入描述。