3

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?

3
  • probably you could create a middleware in your proxy server to read all requests and create new requests with existing + modified headers. Commented Mar 24, 2016 at 10:32
  • 1
    Just get a list of all the incoming headers and skip copying the ones to your new request object that you don't want to pass on. request.headers is a object with all the headers. So, you can just call Object.keys() on it to get a list of the headers. Or you can iterate with for. And then when copying to a new request object, just skip the ones you don't want to pass on. Commented Mar 24, 2016 at 11:06
  • Thanks for the replies. Yeah I was wondering if that would be the way to go, I thought maybe there was something in HTTP, or just some other clever way I hadn't thought of :) Commented Mar 25, 2016 at 7:05

1 Answer 1

5

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");
});
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.