I have a ASP.NET Web API website, sometimes I just want to see the data returned by HTTP GET
. While I know how to get XML or JSON through programming, I don't know where to change the browser's settings (IE, Firefox, and Chrome) to request XML or JSON?
ASP.NET Web API returns JSON or XML based on Accept
header.
Different browsers have different default Accept
headers. As for me:
Firefox:
text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,image/jpeg,image/gif;q=0.2,*/*;q=0.1
As text/xml
is the most preferable, WebAPI returns XML for Firefox.
IE:
application/x-ms-application, image/jpeg, application/xaml+xml, image/gif, image/pjpeg, application/x-ms-xbap, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*
Chrome:
*/*
Since XML and JSON are equally acceptable, WebAPI chooses JSON.
IE generates Accept
header value based on registry keys:
HKLM\Software\Microsoft\Windows\CurrentVersion\Internet Settings\Accepted Documents
Firefox stores it in a variable Network.http.accept.default
.
You can change it in about:config
tab.
I haven't found an information about Chrome. Probably, you can use ModHeader extension to do this.
I am building a WebAPI using Visual Studio 2015. I'm using Firefox to test it. I ran the basic project and attempted to call the following WebAPI request to get an object back (serialized as JSON). When I made the call via Firefox I got an error because Firefox requests XML and WebAPI attempts to default the object to JSON but has no concept for auto-generating the XML.
Resolution
To Fix this you have to go to the Navigation bar of firefox and type :
about:config<ENTER>
You will see a warning screen. Click the button to continue.
You will then see a list of configurable items.
We want the network.http.accept.default
.
It's easiest to find if you type the word accept in the top search bar.
Once you find that item. Double-click it and you will be able to edit it. We just want to add a value that will tell it request JSON as the default. This header will tell the server that JSON is okay. Since the WebAPI test does not set a specific type to return it allows the browser to control it.
When you double-click the item a dialog box with a edit box will popup. You want to simply add the following string to the beginning of the existing string.
application/json,
Make sure you give it a trailing comma to separate it from the other existing values. Click the OK button.
As soon as you make that change (clicking OK to finalize it) you can go back to your original tab and make the call to the API method again and it will return JSON as you expect.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With