Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Firefox requests XML and IE requests JSON from my Web API website, what to change browser's setting?

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?

like image 563
ZZZ Avatar asked Dec 24 '22 15:12

ZZZ


2 Answers

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.

How to change these settings

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.

like image 151
Yeldar Kurmangaliyev Avatar answered Dec 27 '22 12:12

Yeldar Kurmangaliyev


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. firefox 0

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.

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. add value

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.

solution works

like image 32
raddevus Avatar answered Dec 27 '22 10:12

raddevus