POSTMAN POST Request Returns Unsupported Media Type
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.
coolhand
Updated on June 16, 2022Comments
-
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 about 6 yearsMaybe 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 theContent-Type
key withJSON(application/json)
as the value. -
T.Aoukar about 6 years@KirkLarkin Thanks for noting that, didn't notice the =. Fixed answer accordingly.
-
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 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 about 6 yearsmaybe this comments can be included in the answer for next persons that finds this problem
-
Johan Van Wambeke almost 5 yearsHow to i test uploading a file with postman in this situation?
-
Chris Schaller over 2 yearsUploading a file generally requires you to use the Binary body type, but is out of scope for this question @JohanVanWambeke
-
Chris Schaller over 2 yearsYou 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.