Node.js请求:HPE_INVALID_HEADER_TOKEN

Flu*_*ffy 7 http request node.js

HPE_INVALID_HEADER_TOKEN在使用request模块的特定页面上收到。根据我在Google上发现的信息,这是由服务器响应错误/格式错误引起的,但是后者不在我的控制范围内。我可以将请求配置为忽略无效的标头,还是只给我整个原始响应进行处理?

Mar*_*rov 8

The solution is this library: https://www.npmjs.com/package/http-parser-js

So to fix your problem:

  1. npm install http-parser-js

  2. Add this code before require('request')

    process.binding('http_parser').HTTPParser = require('http-parser-js').HTTPParser;
    
    Run Code Online (Sandbox Code Playgroud)

  • 这个答案似乎不再起作用了。Node v12 引入了新的“llhttp”解析器 (2认同)

Dam*_*ien 5

节点v12具有一个新的解析器,该解析器的标头验证更加严格,可能导致相同的错误,尤其是对于使用Imperva / Incapsula的网站,该网站在HTTP 1.1响应中包含无效的标头。

临时解决方案是--http-parser=legacy在节点的命令行或NODE_OPTIONS环境变量中使用该选项。