Node-http-proxy: Running 'setHeader' during 'proxyReq' event fails for POST requests

Created on 12 May 2017  Â·  4Comments  Â·  Source: http-party/node-http-proxy

Hello,

I'm running into the same issue that happened in #908 which was supposed to have been solved already, but it's occurring for me now on the latest v1.16.2. Trying to do a setHeader on the passed in proxyReq for a POST requests fails with:

Error: Can't set headers after they are sent.

It's happening for both node 4.3.2 and 6.1.0.

To reproduce:

proxy.on('proxyReq', function(proxyReq, req, res, options) {
  proxyReq.setHeader('origin', 'https://example.com');
});

Thank you.

Most helpful comment

Most of the times the problem is because you're subscribed to the events more than once. proxy.once('proxyReq,…) usually solves the problem guys.

All 4 comments

I've found a workaround:

proxyReq.headers = proxyReq.headers || {}
proxyReq.headers['x-my-header'] = 'value'

Most of the times the problem is because you're subscribed to the events more than once. proxy.once('proxyReq,…) usually solves the problem guys.

I would like to change headers and path on proxyReq like the following code

proxyReq.path="/abcd/abc" 

It works well with GET requests but it doesn't with POST requests

in post requests _headerSent is true

In my case the problem was an Expect: 100-continue header sent by the client. See https://github.com/http-party/node-http-proxy/issues/1219

Was this page helpful?
0 / 5 - 0 ratings

Related issues

catamphetamine picture catamphetamine  Â·  6Comments

DominicTobias picture DominicTobias  Â·  3Comments

lgzhang picture lgzhang  Â·  4Comments

SET001 picture SET001  Â·  5Comments

sisyphsu picture sisyphsu  Â·  3Comments