【问题标题】:undici node make api request with header for Authorizationundici 节点发出带有授权标头的 api 请求
【发布时间】:2022-11-18 07:15:05
【问题描述】:

我正在尝试使用 undici 发出 api 请求

const apiRequest = await request("https://example.com")
const apiJson = await apiRequest.body.json()
const info = apiJson.data

到目前为止有效... 但是对于我想使用的另一个 API,验证是通过标头完成的。

Authorization: xxxxxx-xxxxxx-xxxxxx

但我无法在文档中找到它是如何与 undici 一起工作的

【问题讨论】:

    标签: node.js api request undici


    【解决方案1】:

    可以将授权标头添加到请求中,如下所示:

    const apiRequest = await request('https://example.com', {
      headers: { Authorization: 'xxxxxx-xxxxxx-xxxxxx' },
    });
    const apiJson = await apiRequest.body.json();
    const info = apiJson.data;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-08-07
      • 1970-01-01
      • 2021-03-24
      • 2017-10-10
      • 2020-08-01
      • 1970-01-01
      相关资源
      最近更新 更多