ATHER
ATHER

Reputation: 3384

ASP core Web API post always giving null data

Here is my Model

    public class Country
{
    public Guid Id { get; set; }
    public string Name { get; set; }
    public string Abbreviation { get; set; }

}

Here is my controller

 [Route("api/[controller]")]
public class TestController : Controller
{
    public TestController()
    {

    }


[HttpPost]
 public IActionResult Create([FromBody] Country country)
    {
            return StatusCode(500, "");
    }

}

When i test my controller through PostMan using following data, why country object is always null. Here is the data i am sending through postman. My break point is hitting that means API is running fine, its just that i am not getting my data there.

I selected "raw" and JSON("application/json") on POSTMAN to send following data

{
     "id": "1",
     "name": "ddd",
     "abbreviation": "ate"
}

Upvotes: 1

Views: 96

Answers (1)

Dale
Dale

Reputation: 1941

Id is a GUID but you are passing it an Integer you have two options either change id to an integer like:

public class Country
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string Abbreviation { get; set; }
}

or pass a GUID via postman.

{
 "id": "593d7c22-893c-474f-9df8-00e219074cb8",
 "name": "ddd",
 "abbreviation": "ate"
}

Upvotes: 2

Related Questions