I cannot create a POST request using POSTMAN, but I have no problems doing it on Swagger

I have a Controller that can receive Post requests.

public IHttpActionResult PostCreaAlimento([FromBody] Alimento alimento)
    {
        if (!ModelState.IsValid)
            return BadRequest("Invalid data.");

        return _repositorio.CreaAlimento(alimento) ? Ok(alimento) : (IHttpActionResult)NotFound();
    }

The request is correctly done on swagger, and I receive a response code 200.

But, if I try to do the same on POSTMAN, I receive an Unsupported Media File.

Request on POSTMAN

Why is this not working :/

>Solution :

Since you are using [FromBody] in the action it means that you need to send application/json contentType
so select Body=> raw => json and put this in the body of Postman

{
    "AlimentoId":4,
    "Description":"testing",
    "Calorias" :400
}

Leave a Reply