Q:

Post request to WebAPI2

I writing REST service using WebAPI2
I need to add book to db.
I have this code Model:
 public class Book
{
 public int Id { get; set; }
 public string Name { get; set; }
 public string Author { get; set; }
 public int Year { get; set; }

}

And Controller:
    [HttpPost]
    public void CreateBook([FromBody]Book book)
    {
        db.Books.Add(book);
        db.SaveChanges();
    }

I try to send POST request from postman

But I have this error 

"Message": "The request entity's media type 'multipart/form-data' is not supported for this resource.",
      "ExceptionMessage": "No MediaTypeFormatter is available to read an object of type 'Book' from content with media type 'multipart/form-data'."

How I can hadle it?

A:

You are getting this error because the Content-Type header being sent with the request is multipart/form-data instead of application/json.  This is happening because you have selected the "form-data" radio button in Postman, which automatically sets the Content-Type to multipart/form-data regardless of whether you have entered a header manually.  (This is covered in the documentation-- see "Note about headers" in the "Request Body" section about 2/3 of the way down the page.)  Try selecting "raw" instead, and ensure you have added a Content-Type header with a value of application/json.  Then, add your JSON in the Body section.
{
    "Id": 1234,
    "Name": "A Book About Nothing",
    "Author": "Joe Schmoe",
    "Year": 1993
}

A:

you have to add a formatter to the HttpConfiguration object, if this is an ASP.NET MVC project, you will find it in the WebApiConfig.cs class under the Register method
 public class WebApiConfig
    {
        public static void Register(HttpConfiguration config)
        {
            //Routes
            config.Routes.MapHttpRoute(/// your routes);

            //Formatters
            config.Formatters.JsonFormatter.SupportedMediaTypes.Add(new MediaTypeHeaderValue("application/json"));

        }
    }

