8

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?

1 Answer 1

8

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 and form-urlencoded data. If no formatter found, client will receives HTTP error 406 (Not Acceptable).

See more:

https://learn.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://learn.microsoft.com/en-us/aspnet/web-api/overview/formats-and-model-binding/media-formatters

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks. I ended up using this: public HttpResponseMessage Post(HttpRequestMessage r) { } This allowed me to get access to the raw xml coming in which I could then parse. I'm sure it's not the "right" way - but it works for me

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.