Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add custom headers to 'request'

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.

like image 534
Ilja Avatar asked Jul 12 '16 14:07

Ilja


People also ask

How do you add a header to a python request?

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.

What are custom headers in API?

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.

How do I send a header in HTTP request?

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.

How do I pass a header in GET 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.

How do I add a custom HTTP header to my website?

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.

How do I add a custom response header to a page?

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.

How to add custom headers to a request for debugging?

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.

How do I send custom headers to outgoing requests?

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.


2 Answers

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)
  })
like image 71
Ilja Avatar answered Oct 21 '22 03:10

Ilja


for some weird reasons req.setHeader('someHeader', 'somValue') didn't work for me.

but req.headers['someHeader'] = 'someValue' this worked

like image 24
kheengz Avatar answered Oct 02 '22 13:10

kheengz