Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nodejs request: HPE_INVALID_HEADER_TOKEN

I receive HPE_INVALID_HEADER_TOKEN on a certain page using request module. From what I've found on Google, this is caused by an incorrect / malformed server response, however the latter is not under my control. Can I configure request to ignore invalid headers or just give me the whole raw response for processing?

like image 320
Fluffy Avatar asked Apr 14 '16 16:04

Fluffy


2 Answers

Node v12 has a new parser that is more strict with header validation, which can cause the same errors, especially with sites using Imperva/Incapsula that include an invalid header in HTTP 1.1 responses.

A temporary solution is to use the --http-parser=legacy option in node's command line, or in the NODE_OPTIONS environment variable.

Since v12.15.0 and v10.19.0 you can do this:

http.request(url, { insecureHTTPParser: true })

Additional information can be found here: https://github.com/nodejs/node/issues/27711#issuecomment-584621376

Edit: --http-parser=legacy is no longer supported in recent versions (including minor versions of v12). A solution for newer node versions is to use --insecure-http-parser instead.

like image 51
Damien Avatar answered Sep 19 '22 11:09

Damien


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;
    
like image 35
Martin Todorov Avatar answered Sep 18 '22 11:09

Martin Todorov