C
C#2y ago
sonodan.

HttpGet API Endpoint Accepting Array Parameter

I have an endpoint for an HttpGet method:
[Route("fightIds")]
[HttpGet]
public async Task<IActionResult> GetPredictionsByFight([FromQuery] IEnumerable<int> fightIds)
{
var predictions = await _predictionService.GetPredictionsByFights(fightIds);

return new OkObjectResult(predictions);
}
[Route("fightIds")]
[HttpGet]
public async Task<IActionResult> GetPredictionsByFight([FromQuery] IEnumerable<int> fightIds)
{
var predictions = await _predictionService.GetPredictionsByFights(fightIds);

return new OkObjectResult(predictions);
}
Is it best practice to use [FromQuery] and accept the values as part of the URI? If so, what's the best way to build this request URI for my HttpClient? Do I need to manually concatenate a string, e.g. 'https://localhost:7109/api/Predictions/fightIds?fightIds=1&fightIds=2'? This feels like I'm doing something that is bad practice here. Or should my endpoint be accepting [FromBody] or some other way?
2 Replies
atakancracker
atakancracker2y ago
Array in Query can cause problems. What if there will be 300 items ? HttpPost with Body is fine
sonodan.
sonodan.2y ago
Thanks - I'll implement it with a body instead!