Answered by:
What is default response type of web api ?

Question
-
User264732274 posted
when we call web api from c# client then we pass content type and based on that response is return in a specific format.
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("url");//post url // set post headers request.Method = "POST"; request.KeepAlive = true; request.Proxy.Credentials = System.Net.CredentialCache.DefaultCredentials; request.UseDefaultCredentials = true; request.Credentials = new NetworkCredential("username", "password", "domain"); request.ContentType = "application/json; charset=utf-8";
if we comment this line request.ContentType = "application/json; charset=utf-8"; then what will be default format of web api response ?
would it be default json or xml ?
Thursday, August 11, 2016 1:24 PM
Answers
-
User-474980206 posted
The ContentType defines the mime type of the post body. The default if not set is form URL encoded. This controls how webapi will parse the request, but not how if formats the response, unless you added code to do that. The default response type is JSON.
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Thursday, August 11, 2016 2:03 PM -
User36583972 posted
Hi sudip_inn,
Firstly, your Web API should support some media-type (Web API provides media-type formatters for both JSON and XML and etc)
JSON and XML Serialization in ASP.NET Web API:
http://www.asp.net/web-api/overview/formats-and-model-binding/json-and-xml-serialization
Then, you can use request.Accept in HttpWebRequest when calling from the client side .
request.Accept = "application/json,application/xml";
How To Get ASP.NET Web API to Return JSON Instead of XML in a Browser:
http://travis.io/blog/2015/10/13/how-to-get-aspnet-web-api-to-return-json-instead-of-xml-in-browser/
Best Regards,
Yohann Lu
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Friday, August 12, 2016 5:23 AM -
User-1144617302 posted
Hi bruce, please clarify, the default return type is "text/html", not json
NEXT,
I believe you are talking about Microsoft ASP.NET WEB API 2.
With this I can surely say the default return type of Web API is which is also known as SupportedMediaTypes is "text/xml"
That means the return type will be xml regardless whatever client you have and use mechanism to call the api.
The WEB API if you want to return JSON then way is>
config.Formatters.JsonFormatter.SupportedMediaTypes.Add(new MediaTypeHeaderValue("text/html"));
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Saturday, August 13, 2016 4:48 PM
All replies
-
User-474980206 posted
The ContentType defines the mime type of the post body. The default if not set is form URL encoded. This controls how webapi will parse the request, but not how if formats the response, unless you added code to do that. The default response type is JSON.
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Thursday, August 11, 2016 2:03 PM -
User264732274 posted
when calling from client side how could i mention in code the expected response type. suppose i want response in json or xml that how could i mention when calling by http client class or HttpWebRequest class ?
thanks
Thursday, August 11, 2016 2:50 PM -
User36583972 posted
Hi sudip_inn,
Firstly, your Web API should support some media-type (Web API provides media-type formatters for both JSON and XML and etc)
JSON and XML Serialization in ASP.NET Web API:
http://www.asp.net/web-api/overview/formats-and-model-binding/json-and-xml-serialization
Then, you can use request.Accept in HttpWebRequest when calling from the client side .
request.Accept = "application/json,application/xml";
How To Get ASP.NET Web API to Return JSON Instead of XML in a Browser:
http://travis.io/blog/2015/10/13/how-to-get-aspnet-web-api-to-return-json-instead-of-xml-in-browser/
Best Regards,
Yohann Lu
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Friday, August 12, 2016 5:23 AM -
User264732274 posted
If i use httpclient class then how could i set accept for xml or json ?Friday, August 12, 2016 4:14 PM -
User-1144617302 posted
Hi bruce, please clarify, the default return type is "text/html", not json
NEXT,
I believe you are talking about Microsoft ASP.NET WEB API 2.
With this I can surely say the default return type of Web API is which is also known as SupportedMediaTypes is "text/xml"
That means the return type will be xml regardless whatever client you have and use mechanism to call the api.
The WEB API if you want to return JSON then way is>
config.Formatters.JsonFormatter.SupportedMediaTypes.Add(new MediaTypeHeaderValue("text/html"));
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Saturday, August 13, 2016 4:48 PM -
User-1144617302 posted
=
Saturday, August 13, 2016 4:49 PM