MAC os curl optional parameter in express.js API route

Issue

I am trying to figure out how i can use the ‘curl’ command in the terminal with an API route that i have built within my server.js file that allows me to insert an optional parameter.

here is my code snippet, hope this is enough,

    app.get('/teams/:id/statistics', async (req, res) => {
  const season = req.query.season || 2021;
  if (isNaN(req.params.id)) {
    res.status(400).json({"error": "Id must be a number"})
    return;
  }
  if (isNaN(season) && season.length !== 4) {
    res.status(400).json({"error": "Invalid season"})
    return;
  }
  const datas = await rapidapi.teams.getTeamStatisticsById(req.params.id, season);
  if (datas.error) {
    res.status(400).json(datas.error);
    return;
  }
  const data = datas[0];
  const teamStats = new TeamStatistics(req.params.id,
   data.games, data.points, data.fgm, data.fga, data.fgp,
    data.ftm, data.gta, data.ftp, data.totreb, data.assists,
     data.steals, data.turnovers, data.blocks)
  res.json(teamStats);
})

Notice how season is defined inside the route as an optional parameter, it can be either req.query.season or it will standardly be set to 2021.

My question is how would i implement this optional parameter in a curl call in my terminal.

Currently, ‘curl http://localhost:xxxx/teams/12/statistics’ runs fine to get data from a team that is on season 2021. How can i add the query parameter of say, "2020" to this request in curl? Is that even possible? I’m wondering because i’m trying to test my route validation.

Solution

as mentioned by Barry Carter in the comments, simply adding to the curl in the terminal like it was any other http request will suffice for equipping optional parameters to your request. Add something like ?season=2020 for standard HTTP query parameters.

Answered By – Claude M

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published