Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NodeJS HTTP - Removing a header from request to be proxied

Tags:

node.js

proxy

I have a NodeJS proxy service which obfuscates some data and forwards the request to another service. Due to some details surrounding how we, and the servcie we're proxying to handle authentication, we need to remove a certain header from the incoming request before we proxy it.

I saw some documentation about request such as: "This object is created internally and returned from http.request(). It represents an in-progress request whose header has already been queued. The header is still mutable using the setHeader(name, value), getHeader(name), removeHeader(name) API."

But then the same documentation says the headers are read-only. I also saw some documentation that showed those methods (removeHeader, etc) being available, and others that don't list it.

Can someone tell me if there's a way to remove a header from the request object itself before copying the headers over? If not is there an easy way to copy over all the headers except the one I want to leave out?

like image 538
ryoaska Avatar asked Dec 11 '25 03:12

ryoaska


1 Answers

Came here looking for a solution, but for node-http-proxy. You can do it by listening to proxyReq event on the proxy and then calling removeHeader on the proxy request object, like so

myProxy.on("proxyReq", function(proxyReq, req, _, options) {
  proxyReq.removeHeader("x-my-header");
});
like image 193
maazadeeb Avatar answered Dec 13 '25 23:12

maazadeeb



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!