【问题标题】:How to add API key in header of request?如何在请求标头中添加 API 密钥?
【发布时间】:2020-04-28 16:46:02
【问题描述】:

我没有后端,需要在标头中创建一个带有 api-key 的 API。

这是客户端指向的路由:

public static async getPlayer(req: Express.Request, res: Express.Response) {
    try {
      const contestantId = req.query.id;
      const playerData = await DataStatisticsService.getPlayerData(contestantId);
      res.send(playerData);
    } catch (err) {
      ErrorHandler.handle('Error fetching player data', err, res);
    }
  }

此 API 调用 getPlayerData(contestantId); 应发送带有 api-key 标头的请求:

 public static async getPlayerData(contenstantId: string): Promise<any> {
    const url = `${BASE_URL}/api/sports/football/players/${contenstantId}/data`;
    const response: Promise<any> = (await axios.get(url)).data;
    return response;
  }

如何为此请求添加带有 api-key 的标头getPlayerData()

【问题讨论】:

标签: javascript reactjs typescript axios


【解决方案1】:

您的通话缺少第二个参数:

axios.get(url, data)

例如:

axios.get(url, {
   headers: {
       Authorization: 'Bearer ' + token
       // Other headers here
   }
   // Other data here
}

【讨论】:

    猜你喜欢
    • 2019-05-02
    • 2021-03-21
    • 1970-01-01
    • 2018-07-29
    • 2018-01-21
    • 2021-04-01
    • 2015-08-30
    • 2019-07-16
    • 1970-01-01
    相关资源
    最近更新 更多