Search code examples
jsonasp.net-coreentity-framework-coregetpostman

The x field is required error in EF Core requests


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?


Solution

  • 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.