0
votes

I'm new in working with ASP.net. I have problem with POST request. Perhaps my code is wrong, I don't find where !

Postman sends me the error message when I try a post request :

"ExceptionType":"System.Net.Http.UnsupportedMediaTypeException","StackTrace":" à System.Net.Http.HttpContentExtensions.ReadAsAsync[T](HttpContent content, Type type, IEnumerable1 formatters, IFormatterLogger formatterLogger, CancellationToken cancellationToken)\r\n à System.Web.Http.ModelBinding.FormatterParameterBinding.ReadContentAsync(HttpRequestMessage request, Type type, IEnumerable1 formatters, IFormatterLogger formatterLogger, CancellationToken cancellationToken)"}

Here is my code :

// POST: api/Boooks
[Route("~/api/PostCreate")]
public IHttpActionResult Post([FromBody] Books books)
{
    if (!ModelState.IsValid)
        return BadRequest("Invalid data.");
2
Did you add 'Content-type': 'application/json' header in postman?bobek

2 Answers

0
votes

If you left it at default, then you will probably find that you need to set the application content type in post man as part of your headers info.

By Default I think asp.net core expects the content type to be of application/json. and your post man call is most likely sending it as text/html.

See your headers sent, make the changes and see the results.

0
votes

Check your request headers in Postman!

Check your request headers in Postman

You should not have to change anything in the c# code, it should accept JSON by default.