Add custom headers to 'request'

44,872

Solution 1

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

Solution 2

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

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

Share:
44,872
Ilja
Author by

Ilja

That dev at the local ☕️ shop sipping on late.

Updated on May 15, 2021

Comments

  • Ilja
    Ilja almost 3 years

    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.