.Net Core WebAPI, Unable to send data using POSTMAN, error - 415 Unsupported MediaType

I am testing my first .net Core WebAPI with Postman

Unknown media type error.

What am I missing?

This is a postman rest client

This is my publication object.

public class Country { [Key] public int CountryID { get; set; } public string CountryName { get; set; } public string CountryShortName { get; set; } public string Description { get; set; } } 

This is the webapi controller

 [HttpPost] public async Task<IActionResult> PostCountry([FromBody] Country country) { if (!ModelState.IsValid) { return BadRequest(ModelState); } _context.Country.Add(country); try { await _context.SaveChangesAsync(); } catch (DbUpdateException) { if (CountryExists(country.CountryID)) { return new StatusCodeResult(StatusCodes.Status409Conflict); } else { throw; } } return CreatedAtAction("GetCountry", new { id = country.CountryID }, country); } 
+7
rest asp.net-core-mvc asp.net-web-api2 postman
source share
2 answers

You are not sending a Content-Type header. Select JSON (application/json) from the drop-down list next to the mouse pointer in the first screenshot: Like this

+18
source share

This worked for me (I used api on the route)

 [Produces("application/json")] [Route("api/Countries")] public class CountriesController : Controller { // POST: api/Countries [HttpPost] public async Task<IActionResult> PostCountry([FromBody] Country country) { if (!ModelState.IsValid) { return BadRequest(ModelState); } _context.Country.Add(country); try { await _context.SaveChangesAsync(); } catch (DbUpdateException) { if (CountryExists(country.CountryID)) { return new StatusCodeResult(StatusCodes.Status409Conflict); } else { throw; } } return CreatedAtAction("GetCountry", new { id = country.CountryID }, country); } } 

enter image description here

0
source share

All Articles