skip to Main Content

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:

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


  1. Chosen as BEST ANSWER

    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.


  2. 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 below

    [HttpGet(Name = "listById")]
    public async Task<IActionResult> ListById([FromQuery] ulong[]? idList) =>
        await Handle(Service.FindByList(idList));
    

    Moreover, you must call the GET action above like the following URL with query parameters

    yourdomain.com/…./listById?idList=1&idList=2&idList=3

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search