POSTMAN POST Request Returns Unsupported Media Type

13,791

When using Postman with POST and JSON body you'll have to use the raw data entry and set it to application/json and data would be like this:

{"clientName":"Anne", "location":"Meeting Room 4"}

Note how both key and value are quoted.

Share:
13,791
coolhand
Author by

coolhand

Updated on June 16, 2022

Comments

  • coolhand
    coolhand almost 2 years

    I am following the API instructions from Adam Freeman's "Pro ASP.NET Core MVC 2". I have the following API controller class:

        [Route("api/[controller]")]
        public class ReservationController : Controller
        {
            private IRepository repository;
    
        public ReservationController(IRepository repo) => repository = repo;
    
        [HttpGet]
        public IEnumerable<Reservation> Get() => repository.Reservations;
    
        [HttpGet("{id}")]
        public Reservation Get(int id) => repository[id];
    
        [HttpPost]
        public Reservation Post([FromBody] Reservation res) =>
            repository.AddReservation(new Reservation
            {
                ClientName = res.ClientName,
                Location = res.Location
            });
    
        [HttpPut]
        public Reservation Put([FromBody] Reservation res) => repository.UpdateReservation(res);
    
        [HttpPatch("{id}")]
        public StatusCodeResult Patch(int id, [FromBody]JsonPatchDocument<Reservation> patch)
        {
            Reservation res = Get(id);
            if(res != null)
            {
                patch.ApplyTo(res);
                return Ok();
            }
            return NotFound();
        }
    
        [HttpDelete("{id}")]
        public void Delete(int id) => repository.DeleteReservation(id);
    }
    

    The text uses PowerShell to test the API but I would like to use Postman. In Postman, the GET call works. However, I cannot get the POST method to return a value. The error reads 'Status Code: 415; Unsupported Media Type'

    In Postman, the Body uses form-data, with:

    key: ClientName, value: Anne
    key: Location, value: Meeting Room 4
    

    If I select the Type dropdown to "JSON", it reads "Unexpected 'S'"

    In the Headers, I have:

    `key: Content-Type, value: application/json`
    

    I have also tried the following raw data in the body, rather than form data:

    {clientName="Anne"; location="Meeting Room 4"}
    

    The API controller does work and return correct values when I use PowerShell. For the POST method, the following works:

    Invoke-RestMethod http://localhost:7000/api/reservation -Method POST -Body (@{clientName="Anne"; location="Meeting Room 4"} | ConvertTo-Json) -ContentType "application/json"
    
  • coolhand
    coolhand about 6 years
    Maybe I'm doing something wrong, but I still have the same error. In Postmans Body, I used raw and the quotations as you've shown. In the 'Headers' I am using the Content-Type key with JSON(application/json) as the value.
  • T.Aoukar
    T.Aoukar about 6 years
    @KirkLarkin Thanks for noting that, didn't notice the =. Fixed answer accordingly.
  • coolhand
    coolhand about 6 years
    @KirkLarkin, sorry you edited your comment before I could post. I've tried what you suggest and still get the same error
  • T.Aoukar
    T.Aoukar about 6 years
    @coolhand you don't have to manually add the header value, either remove it, or set the value to application/json the JSON(application/json) is the box in Postman when you're entering raw data
  • Mauricio Gracia Gutierrez
    Mauricio Gracia Gutierrez about 6 years
    maybe this comments can be included in the answer for next persons that finds this problem
  • Johan Van Wambeke
    Johan Van Wambeke almost 5 years
    How to i test uploading a file with postman in this situation?
  • Chris Schaller
    Chris Schaller over 2 years
    Uploading a file generally requires you to use the Binary body type, but is out of scope for this question @JohanVanWambeke
  • Chris Schaller
    Chris Schaller over 2 years
    You shouldn't be patching an array, the post should be a wrapped object but your example is an object inside an array, this solution isn't an improvement over the previously accepted answer and is misleading, if it works for you then there is something non-standard about your API implementation.