Click here to Skip to main content
15,883,848 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hey guys, I'm making an asp.net web api using mvc. I have the GET/POST/PUT/DELETE all in the default controller. But what I'm testing now is the post(get is working fine), I'm using the postman extension of chrome to test this and what is happening is that postman is giving me an error due to media types any ideas how to fix this?

POST controller:

// POST: api/Events
        [HttpPost]
        [ResponseType(typeof(Event))]
        public async Task<IHttpActionResult> PostBook(Event events)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            db.Events.Add(events);
            await db.SaveChangesAsync();

            // New code:
            // Load author name
            db.Entry(events).Reference(e => e.Name).Load();

            var dto = new EventDto()
            {
                Name = events.Name,
                Project = events.Project,
                Objectives = events.Objectives,
                City = events.City,
                Country = events.Country,
                EventStart = events.EventStart,
                Departure = events.Departure,
                Arrival = events.Arrival,
                Registration = events.Registration,
                NationalTransportation = events.NationalTransportation,
                Accommodation = events.Accommodation,
                AcNumberNights = events.AcNumberNights,
                AcPreferHotel = events.AcPreferHotel,
                AcPreferHotelUrl = events.AcPreferHotelUrl,
                Flight = events.Flight,
                FlDeparture = events.FlDeparture,
                FlDepartPrefer = events.FlDepartPrefer,
                FlDepartPreferUrl = events.FlDepartPreferUrl,
                FlReturn = events.FlReturn,
                FlRetPrefer = events.FlRetPrefer,
                FlRetPreferUrl = events.FlRetPreferUrl,
                Notes = events.Notes,
                Files = events.Files,
                Status = events.Status
            };
            return CreatedAtRoute("DefaultApi", new { id = events.Id }, dto);
        } 



JSON I'm inserting in postman:

{
"Name": "psilva", 
"Project": "a",
"Objectives": "s",
"City": "a",
"Country": "s",
"EventStart": 2012,
"Departure": 2012,
"Arrival": 2012,
"Registration": "a",
"NationalTransportation": "a",
"Accommodation" : "a",
"AcNumberNights": 1,
"AcPreferHotel": "a",
"AcPreferHotelUrl": "a",
"Flight": "a",
"FlDeparture": 2012,
"FlDepartPrefer": "a",
"FlDepartPreferUrl": "a",
"FlReturn": 2012,
"FlRetPrefer": "a",
"FlRetPreferUrl": "a",
"Notes": "a",
"Files": "a",
"Status": "a"    
}


error from postman:

{
    "Message": "The request entity's media type 'text/plain' is not supported for this resource.",
    "ExceptionMessage": "No MediaTypeFormatter is available to read an object of type 'Event' from content with media type 'text/plain'.",
    "ExceptionType": "System.Net.Http.UnsupportedMediaTypeException",
    "StackTrace": "   at System.Net.Http.HttpContentExtensions.ReadAsAsync[T](HttpContent content, Type type, IEnumerable`1 formatters, IFormatterLogger formatterLogger, CancellationToken cancellationToken)\r\n   at System.Net.Http.HttpContentExtensions.ReadAsAsync(HttpContent content, Type type, IEnumerable`1 formatters, IFormatterLogger formatterLogger, CancellationToken cancellationToken)\r\n   at System.Web.Http.ModelBinding.FormatterParameterBinding.ReadContentAsync(HttpRequestMessage request, Type type, IEnumerable`1 formatters, IFormatterLogger formatterLogger, CancellationToken cancellationToken)"
}


Please help
Posted

1 solution

you need to manually add an 'application/json' header to your post request - I cant remember the details, you add this to your http client iirc
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900