0

I don't backend and need to make an API with api-key in the header.

This is the route which was pointed from the client:

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);
    }
  }

This API call getPlayerData(contestantId); should send request with header which have 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;
  }

How to add header with api-key for this request getPlayerData()?

hoangdv
  • 15,138
  • 4
  • 27
  • 48
Michel James
  • 55
  • 2
  • 7
  • Does this answer your question? [How to set header and options in axios?](https://stackoverflow.com/questions/45578844/how-to-set-header-and-options-in-axios) – takendarkk Apr 28 '20 at 16:47

1 Answers1

0

You're missing the second parameter to your call:

axios.get(url, data)

For example:

axios.get(url, {
   headers: {
       Authorization: 'Bearer ' + token
       // Other headers here
   }
   // Other data here
}
Reece Kenney
  • 2,734
  • 3
  • 24
  • 57