WEBAPI return Data type doubts

Source: Internet
Author: User

Recently started using Webapi, easy to get started, and then some doubts

1.WebAPI What data type is returned by default, JSON or XML?
2. How to modify the return data type of WEBAPI
, I use IE browser to return the data are in JSON format, with Firefox and Chrome return data format is XML, and then the HttpWebRequest request to return the JSON format, I wonder, I am the same program, the same configuration file, Output data format Why do not have the same, even if you default output JSON or XML can be understood, I also different browser, output format is different, after some research finally understand the reason


The test found that the data returned using IE browser is JSON, and the use of Firefox and Chrome returned to XML, the study found that in the HTTP request of IE, the request header Accpet node compared to Firefox and Chrome missing "application/ XML "type, because the WEBAPI return data is in XML or JSON format, IE does not send acceptable XML and JSON type, so the default is JSON format data, and Firefox and Chrome sent an acceptable XML type, so the XML data is returned, the following is IE, Request headers for Firefox and Chrome browser

Browser Return Data format Accept Request Header
Ie Json

Text/html, Application/xhtml+xml, */*

Firefox Xml

text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8,*/*

Chrome Xml

text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8,*/*

Continue with the following tests

1. Send only Accept:application/json, and the result returns JSON

2. Send only Accept:application/xml, the result returns the XML

3. Send Accept:application/xml,application/json at the same time, the result returns the JSON

4. Send Accept:application/json,application/xml at the same time, the result returns the JSON

5. The change priority is sent application/xml;q=1.0,application/json;q=0.9, and the result returns the XML

The conclusion can be drawn from this:

The return data type of the WEBAPI is determined by the accept with the request header, and the default return type is JSON
When 1.application/json and Application/xml are not, return JSON data
2. Return JSON data only when Application/json is available
3. Return XML data only when Application/xml is available
4. When there are both Application/json and Application/json, the return data type is independent of the order of the two, and if both have the same precedence, the JSON is returned, and if the priority is different, the type with high priority is returned.

See the table below:

Accept Header return type
Application/json Json
Application/xml Xml
Application/xml,application/json Json
Application/json,application/xml Json
application/xml;q=1.0,application/json;q=1.0 Json
application/xml;q=0.9,application/json;q=0.9 Json
application/xml;q=1.0,application/json;q=0.9 Xml
application/xml;q=0.9,application/json;q=1.0 Json

WEBAPI return Data type doubts

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.