I'm new to web api and I need to create a server for a client. I have no control over the client - can't change a thing.
The client sends in an html encapsulated json request in a POST body. However, the content-type can vary. What do I need to do to allow my ApiController to process different content-types?
Under the hood, Web Api supports Content Negotiation mechanism to automatically opt the correct formatter based on the header
Content-Type
in HTTP request.By default content negotiation supports three formatters:
json
,xml
andform-urlencoded data
. If no formatter found, client will receives HTTP error 406 (Not Acceptable).See more:
https://docs.microsoft.com/en-us/aspnet/web-api/overview/formats-and-model-binding/content-negotiation
If you need to allow Web Api support another
Content-Type
, you can write your own custom formatter:https://docs.microsoft.com/en-us/aspnet/web-api/overview/formats-and-model-binding/media-formatters