简体   繁体   English

将自定义标头添加到“请求”

[英]Add custom headers to 'request'

I am proxying my api via following setup in my express config我通过在我的快速配置中进行以下设置来代理我的 api

  // 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 ie /users I tried following documentation on npm for request and set up my headers like so这里的config.API_HOST解析为我的 api url, req.url是一些端点,即/users我尝试按照 npm 上的文档进行请求并像这样设置我的标头

  // 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.但是我无法在网络下的 chrome 开发工具中看到我的自定义标头。

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.由于一些奇怪的原因req.setHeader('someHeader', 'somValue')对我不起作用。

but req.headers['someHeader'] = 'someValue' this worked但是req.headers['someHeader'] = 'someValue'这有效

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM