Recently, when using WebAPI, the browser accesses WebAPI, and the default is XML format, I think this returned data format should be customizable by the user, allowing the server to return XML format or JSON format, instead of server code to configure!
Although the server code can be configured, I don't think it was the original intention of WebAPI.
1. What data type does WebAPI return by default, json or xml? 2. How to modify the return data type of WebAPI I use IE browser to request that the data returned is in JSON format, and the data format returned by Firefox and Chrome is XML, and then I use HttpWebRequest to request that the return is JSON format, I wondered, why is the same program, the same configuration file, why is the output data format different, even if you default output json or xml can be understood, why are there different browsers, the output format is different, After some research, I finally figured out the reason
After testing, it was found that the data returned by using IE browser is json, while using Firefox and Chrome is xml, and it is found that IE lacks the "application/xml" type compared with Firefox and Chrome when http requests occur. So the default is json format data, while Firefox and Chrome send acceptable xml types, so xml data is returned, and the following is the request header of IE, Firefox and Chrome
Accept only appears in Requests
What it means: The type of media that is acceptable on the browser side
For example, Accept: text/html means that the browser can accept the type of postback from the server as text/html, which is commonly referred to as html documents
If the server cannot return data of type text/html, the server should return a 406 error (non acceptable)
Wildcard * represents any type
For example, Accept: */* means that the browser can handle all types, (generally the browser sends this to the server)
For example, Accept: text/* means that all subclasses of text are acceptable
Accept can support multiple types of separations
For example, Accept: audio/*; q=0.2, audio/basic is to the effect that browsers prefer audio/basic media types, but if they don't have this type, other audio types can also be used
Here a q=0.2 appears, which is a value that represents the degree of association, and the default value is one, arranged from largest to smallest
For example, Accept: text/plain; q=0.5, text/html, text/x-dvi; q=0.8, text/x-c
The acceptable type priorities are as follows
1) text/html text/x-c
2)text/x-dvi
3)text/plain
q is a value between 0-1, the default value of q is 1, and q=0 means non acceptable
Finally, the test results:
| browser | Return to formatting | accept request header | | ie | json | text/html, application/xhtml+xml, */* | | Chrome | xml | text/html,application/xhtml+xml,application/xml; q=0.9,image/webp,image/apng,*/*; q=0.8 | | Firefox | xml | text/html,application/xhtml+xml,application/xml; q=0.9,*/*; q=0.8,*/* |
Continue with the following tests
1. Only accept:application/json is sent, and the result is returned json
2. Only accept:application/xml is sent, and the result returns xml
3. Send accept: application/xml and application/json at the same time, and the result returns json
4. Send accept:application/json and application/xml at the same time, and the result returns json
5. Modify the priority and send application/xml at the same time. q=1.0,application/json; q=0.9, and the result returned xml
From this it can be concluded:
The return data type of WebAPI is determined by the accept of the request header, and the default return type is json 1. If neither application/json nor application/xml exists, return the json data 2. When only application/json is available, return the json data 3. When only application/xml is available, the xml data is returned 4. When there is application/json and application/json at the same time, the returned data type is irrelevant to the order of the two, if the two have the same priority, json is returned, and if the priority is different, the type with higher priority is returned
| Accept head | 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 |
As shown below:
|