Ilja Ilja - 1 year ago 157
Node.js Question

Unable to get referer headers working

I have a proxy set up for a third party service that at the moment looks like this:

app.use('/service', (req, res) => {
let url = `${config.POSTCODER_KEY}${req.url}`
req.headers['Referer'] = ''

As you can see I am trying to add Referer header to the request and it seems to be working as
prints out
however request fails and the error I get back from the service is 403 unauthorised referring to Referer header. When I inspect network in inspector tools my referer is displayed as localhost.

I am testing this in Postman api client ( by setting Referer to my white listed domain and it works. I'm not sure why it uses localhost with express.

Answer Source

Piping streams together only transfers the data in those streams. Headers are not a part of that. When you req.pipe(request(url)) you're only writing the request body to the proxied request. If you want to set the headers used for the proxied request, you have to pass them to request, like:

req.pipe(request({ url: url, headers: req.headers })).pipe(res);

However, as noted in my answer to your previous question, you will also need to properly set the headers on res when the proxied response arrives.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download