Search code examples
javascriptnode.jsexpresshttp-headershttprequest

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.


Solution

  • 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)
      })