Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Running 'setHeader' during 'proxyReq' event fails for POST requests #1168

Open
lrettig opened this issue May 12, 2017 · 4 comments
Open

Running 'setHeader' during 'proxyReq' event fails for POST requests #1168

lrettig opened this issue May 12, 2017 · 4 comments

Comments

@lrettig
Copy link

lrettig commented May 12, 2017

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.

@leth
Copy link

leth commented May 19, 2017

I've found a workaround:

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

thiagobustamante added a commit to thiagobustamante/node-http-proxy that referenced this issue May 26, 2017
This PR tries to fix "Can't set headers after they are sent" errors.
That are a lot of situations where this error can occurs. In my case, it is happening because I have others middlewares (in an expressjs application that tries to proxy requests). Some of those middlewares (like [passportjs](http://passportjs.org/), or [cors](https://www.npmjs.com/package/cors)) can run ```res.end()``` and when the proxy receive a response, it is already finished.
So, it is necessary to test if we can write on the user response when the proxy response is ready.
I think it could also fix http-party#930, http-party#1168, http-party#908
jcrugzz pushed a commit that referenced this issue Apr 19, 2018
This PR tries to fix "Can't set headers after they are sent" errors.
That are a lot of situations where this error can occurs. In my case, it is happening because I have others middlewares (in an expressjs application that tries to proxy requests). Some of those middlewares (like [passportjs](http://passportjs.org/), or [cors](https://www.npmjs.com/package/cors)) can run ```res.end()``` and when the proxy receive a response, it is already finished.
So, it is necessary to test if we can write on the user response when the proxy response is ready.
I think it could also fix #930, #1168, #908
jcrugzz pushed a commit that referenced this issue Apr 20, 2018
This PR tries to fix "Can't set headers after they are sent" errors.
That are a lot of situations where this error can occurs. In my case, it is happening because I have others middlewares (in an expressjs application that tries to proxy requests). Some of those middlewares (like [passportjs](http://passportjs.org/), or [cors](https://www.npmjs.com/package/cors)) can run ```res.end()``` and when the proxy receive a response, it is already finished.
So, it is necessary to test if we can write on the user response when the proxy response is ready.
I think it could also fix #930, #1168, #908
indexzero pushed a commit that referenced this issue Apr 20, 2018
This PR tries to fix "Can't set headers after they are sent" errors.
That are a lot of situations where this error can occurs. In my case, it is happening because I have others middlewares (in an expressjs application that tries to proxy requests). Some of those middlewares (like [passportjs](http://passportjs.org/), or [cors](https://www.npmjs.com/package/cors)) can run ```res.end()``` and when the proxy receive a response, it is already finished.
So, it is necessary to test if we can write on the user response when the proxy response is ready.
I think it could also fix #930, #1168, #908
@XVincentX
Copy link

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.

@fingerpich
Copy link

fingerpich commented Nov 4, 2018

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

@skleeschulte
Copy link

In my case the problem was an Expect: 100-continue header sent by the client. See #1219

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants