node-http-proxy icon indicating copy to clipboard operation
node-http-proxy copied to clipboard

proxyReq.setHeader does not work

Open wwhgtt opened this issue 6 years ago • 5 comments

here is my code const HttpProxy = require('http-proxy') const proxy = HttpProxy.createProxyServer() proxy.on('proxyReq', function(proxyReq, req, res) { proxyReq.setHeader('cookie', req.headers.authorization); console.log('proxyReq', req.headers); }); proxy.web(ctx.req, ctx.res, pxy.proxy)

request method is GET

my target is add cookie on request header

wwhgtt avatar Sep 20 '19 03:09 wwhgtt

So I did similar things and after my proxyReq.setHeader line I did a console.log(JSON.stringify(proxyReq.headers)). Guess what, it prints undefined!

And later I used more console.log line all around the place and found that the the "proxyReq" event is actually fired after the .web request.

Simply put console.log lines before and after proxy.web line and you will see what I am saying.

cwyl02 avatar Sep 23 '19 21:09 cwyl02

I am running into the same issue.

claudio-viola avatar Sep 24 '19 15:09 claudio-viola

Same. Tried to write Set-Cookie header to res and it doesn't work

Disorrder avatar Oct 17 '19 08:10 Disorrder

A workaround may be to delete the original header in proxyReq. Worked for an issue I had to rewrite content-type header.

robhop avatar Aug 05 '20 14:08 robhop

A workaround may be to delete the original header in proxyReq.解决方法可能是删除 proxyReq 中的原始标头。 Worked for an issue I had to rewrite content-type header. 解决了一个问题,我必须重写内容类型标题。

thanks for this. i have same question in response header,worked for me to remove header in proxyRes.

          configure: (proxy, options) => {
            proxy.on('proxyRes', (proxyRes, req, res) => {
              proxyRes.headers['content-type'] = undefined;
              res.removeHeader('Content-Type');
              res.setHeader('Content-Type', 'image/svg+xml');
              proxyRes.pipe(res);
            });
          },

brightzoe avatar Oct 11 '23 06:10 brightzoe