Add custom headers to 'request'

Report
Question

Please briefly explain why you feel this question should be reported .

Report Cancel

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.

solved 0
1 Answer 9 views 0

Answer ( 1 )

    0
    December 16, 2016 at 8:29 am

    Please briefly explain why you feel this answer should be reported .

    Report Cancel

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

Leave an answer

Browse

What is the capital of Egypt ? ( Cairo )

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>