I am proxying my api via following setup in my express config
// Proxy api calls
app.use('/api', function (req, res) {
let url = config.API_HOST + req.url
req.pipe(request(url)).pipe(res)
})
config.API_HOST
in here resolves to my api url and req.url
is some endpoint i.e. /users
I tried following documentation on npm for request and set up my headers like so
// Proxy api calls
app.use('/api', function (req, res) {
let options = {
url: config.API_HOST + req.url,
options: { 'mycustomheader': 'test' }
}
req.pipe(request(options)).pipe(res)
})
But I am not able to see my custom headers in chrome dev tools under Network.
To add HTTP headers to a request, you can simply pass them in a dict to the headers parameter. Similarly, you can also send your own cookies to a server using a dict passed to the cookies parameter.
Custom Headers allow us to add extra content to our HTTP requests and responses, which we can pass between the client and server. We can use custom headers for metadata, such as defining the current version of the API that is being used.
To add custom headers to an HTTP request object, use the AddHeader() method. You can use this method multiple times to add multiple headers. For example: oRequest = RequestBuilder:Build('GET', oURI) :AddHeader('MyCustomHeaderName','MyCustomHeaderValue') :AddHeader('MySecondHeader','MySecondHeaderValue') :Request.
For example, to send a GET request with a custom header name, you can use the "X-Real-IP" header, which defines the client's IP address. For a load balancer service, "client" is the last remote host. Your load balancer intercepts traffic between the client and your server.
In the Home pane, double-click HTTP Response Headers. In the HTTP Response Headers pane, click Add... in the Actions pane. In the Add Custom HTTP Response Header dialog box, set the name and value for your custom header, and then click OK. Required string attribute.
In the HTTP Response Headers pane, click Add... in the Actions pane. In the Add Custom HTTP Response Header dialog box, set the name and value for your custom header, and then click OK. Required string attribute. Specifies a field name for the custom response header.
At times we may need to add custom headers to a request for debugging purpose. e.g. You may want to display debugging messages when specific header is present in request (e.g. debug with value 1). This can be done using Modify header chrome plugin. Here are quick steps: Install the Modify header plugin in Chrome browser.
One can also use HttpContext to access required headers and verify. Adding HeaderPropogation Middleware is also a very simple way of adding the headers and propagating them to the next request. Please see here below article on how to use HeaderPropogation Middleware to send custom headers to outgoing requests.
Was able to achieve it this way
app.use('/api', function (req, res) {
let url = config.API_HOST + req.ur
req.headers['someHeader'] = 'someValue'
req.pipe(request(url)).pipe(res)
})
for some weird reasons req.setHeader('someHeader', 'somValue')
didn't work for me.
but req.headers['someHeader'] = 'someValue'
this worked
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