Before I proxy to a address I want to set the header of the proxy (Smth like an interceptor). I use the express-http-library and express with Node.JS. So far my code looks as follow. Btw. the docs of this library did not make me any wiser.
app.use('/v1/*', proxy('velodrome.usefixie.com', {
userResHeaderDecorator(headers, userReq, userRes, proxyReq, proxyRes) {
// recieves an Object of headers, returns an Object of headers.
headers = {
Host: 'api.clashofclans.com',
'Proxy-Authorization': `Basic ${new Buffer('token').toString('base64')}`
};
console.log(headers);
return headers;
}
}));
And even though the console prints me out the headers obj. as expected the proxy authorization did not work:
{ Host: 'api.clashofclans.com',
'Proxy-Authorization': 'Basic token' }
Can anyone help me out?
express-http-proxy
allows you to pass in an options object (same object as used in the request
library) via proxyReqOptDecorator
:
app.use("/proxy", proxy("https://target.io/api", {
proxyReqOptDecorator: function (proxyReqOpts, srcReq) {
proxyReqOpts.headers = {"Authorization": "Bearer token"};
return proxyReqOpts;
}
}));
or
app.use("/proxy", proxy("https://target.io/api", {
proxyReqOptDecorator: function (proxyReqOpts, srcReq) {
proxyReqOpts.auth = `${username}:${password}`;
return proxyReqOpts;
}
}));
The documentation for proxyReqOptDecorator
can be found here
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With