If got a controller endpoint expecting a DTO with just a list of Ids to fetch this specific list of IDs. When sending the following request to the endpoint, it returns an error
the idList field is required
I tried sending it multiple ways.
This is the controller method in question:
[HttpGet("listById")]
public async Task<IActionResult> ListById([FromBody] IdListDto idList) =>
await Handle(Service.FindByList(idList));
The DTO:
public record IdListDto : IIdListDto
{
public ulong[]? Ids { get; set; }
}
Postman request and result:
I tried changing the request in different ways, like wrapping the "Ids" parameter in an idList
JSON object, but the result is the same. I don’t actually need the DTO and tried using just a simple array, but it didn’t recognize that either.
Any ideas on how to solve this?
2
Answers
I changed "FromBody" to "FromForm" and now it works. I know this is probably the wrong way to handle this, but I need to move on.
I'll look into it again at a later stage and edit my question with an qualified explanation of what went wrong.
The reason of the issue is using
[FromBody]
in a GET request. You cannot use[FromBody]
in a "GET" request. Simply because GET requests have no body.What you can do is to use
[FromQuery]
instead. Your code should look like the one belowMoreover, you must call the GET action above like the following URL with query parameters
yourdomain.com/…./listById?idList=1&idList=2&idList=3