This article is a mirror article of machine translation, please click here to jump to the original article.

View: 24777|Reply: 3

[WebAPI] WebAPI customizes the return data format

[Copy link]
Posted on 11/30/2017 11:00:16 AM | | | |
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:

browserReturn to formatting  accept request header
iejsontext/html, application/xhtml+xml, */*
Chromexmltext/html,application/xhtml+xml,application/xml; q=0.9,image/webp,image/apng,*/*; q=0.8
Firefoxxmltext/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 headReturn type
application/jsonjson
application/xmlxml
application/xml,application/jsonjson
application/json,application/xmljson
application/xml; q=1.0,application/json; q=1.0json
application/xml; q=0.9,application/json; q=0.9json
application/xml; q=1.0,application/json; q=0.9xml
application/xml; q=0.9,application/json; q=1.0json


As shown below:





Previous:python3 implements concurrent access horizontal partitioning tables
Next:.Net MVC implements long polling
Posted on 11/30/2017 11:06:08 AM |
It's amazing, I found this situation before, but I didn't think why
 Landlord| Posted on 11/30/2017 11:07:36 AM |
QWERTYU Posted on 2017-11-30 11:06
It's amazing, I found this situation before, but I didn't think why

It is recommended to discourage returning XML format because, in some cases, returning JSON format is normal and returning XML format throws an exception

For example, if the return value is object, the return JSON format is normal, and the return XML format will report an error
Posted on 4/25/2018 8:50:44 AM |
The lecture is very detailed, and there are knowledge points, thank you, it has been collected
Disclaimer:
All software, programming materials or articles published by Code Farmer Network are only for learning and research purposes; The above content shall not be used for commercial or illegal purposes, otherwise, users shall bear all consequences. The information on this site comes from the Internet, and copyright disputes have nothing to do with this site. You must completely delete the above content from your computer within 24 hours of downloading. If you like the program, please support genuine software, purchase registration, and get better genuine services. If there is any infringement, please contact us by email.

Mail To:help@itsvse.com