【问题标题】:What is the best way to add a "x-api-key" header to a get request in NodeJS + Express?在 NodeJS + Express 中向 get 请求添加“x-api-key”标头的最佳方法是什么?
【发布时间】:2019-04-06 14:25:36
【问题描述】:

我想发送一个获取请求,但我需要将我的 api 密钥插入到“x-api-key”标头中。我正在使用 NodeJS + Express。现在我正在使用“isomorphic unfetch”库中的 fetch:

https://github.com/developit/unfetch/tree/master/packages/isomorphic-unfetch

我使用它来从 get 请求中获取数据。我特别使用这个库,因为它在服务器和客户端上都运行良好。

我应该如何将标头添加到我的请求中?谢谢!

【问题讨论】:

    标签: node.js api express http-headers isomorphic-fetch-api


    【解决方案1】:

    unfetch repository 中有一个示例展示了如何添加标头来获取请求。

    // complex POST request with JSON, headers:
    fetch('/bear', {
      method: 'POST',
      headers: {
        'Content-Type': 'application/json',
        'x-api-key': 'Bearer XYZ'
      },
      body: JSON.stringify({ hungry: true })
    }).then( r => {
      open(r.headers.get('location'));
      return r.json();
    })
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-01-21
      • 2013-10-28
      • 2020-06-19
      • 2014-09-10
      • 1970-01-01
      • 2018-04-26
      • 2018-11-15
      相关资源
      最近更新 更多