Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RESTful web service returning XML not JSON

I have this simple web service, right now it just looks to see if the part number is A123456789 and then it returns a model number. This will be replaced by logic that will be connecting into a database to check the partno against and then return the actual model number. But at this point I just need it to return some dummy JSON data. However when I use Fiddler and look at the call in the web broswer of http://localhost:PORT/Scan/Model/A123456789 it returns this

<string xmlns="http://schemas.microsoft.com/2003/10/Serialization/">Model: CVS-1679</string>

But when I do a GET in fiddler of the same URI I get

"Model: CVS-1679"

Only under the textview tab.

Why is it being returned in XML (in the browser and text in Fiddler) and not JSON, when I have setup my ResponseFormat to be JSON?

My Code:

[WebGet(UriTemplate = "Model/{partno}", ResponseFormat = WebMessageFormat.Json)]
        public string Model(string partno)
        {
            if (partno == "A123456789")
            {
                string modelno = "CVS-1679";
                return "Model: " + modelno;
            }
            else
            {
                string modelno = "CVS-1601";
                return "Model: " + modelno;
            }
        }
like image 933
atrljoe Avatar asked Dec 01 '11 15:12

atrljoe


People also ask

CAN REST service return XML?

Data types that REST API can return are as follows: JSON (JavaScript Object Notation) XML. HTML.

Does REST use JSON or XML?

Unlike SOAP, REST doesn't have to use XML to provide the response. You can find REST-based web services that output the data in Command Separated Value (CSV), JavaScript Object Notation (JSON) and Really Simple Syndication (RSS).

Does restful API use XML?

The REST API supports the following data formats: application/json. application/json indicates JavaScript Object Notation (JSON) and is used for most of the resources. application/xml indicates eXtensible Markup Language (XML) and is used for selected resources.

Why JSON and XML are used in restful web services?

In REST architecture, a REST Server simply provides access to resources and REST client accesses and modifies the resources. Here each resource is identified by URIs/ global IDs. REST uses various representation to represent a resource like text, JSON, XML. JSON is the most popular one.


2 Answers

ASP.NET webservice return XML / SOAP message by default. In case you want to return Json string, you would need to decorate the Webservice with [ScriptService] attribute. This inform the IIS that this service would be used by ASP.NET AJAX calls. These attribute are part of System.Web.Extensions.

You can define the web method response format by decorating the webmethod with ScriptMethod attribute.

[ScriptMethod(ResponseFormat = ResponseFormat.Json)]

However even after decorating the webservice and webmethod by these attribute, the response can still be in XML format. This behaviour happen when the client which makes the request does not set the Request Header content type as “application/json”.

Before return the method call from webmethod serialize to Json string using JavaScriptSerializer

Debugging WebService using Fiddler

It is quite easy to use fiddler to test webservice. Following figure is an example of how to call a Webservice which returns a json string. Note that the request content type is set to application/json. The parameters expected by webserivce is mentioed in the Request Body section.enter image description here

Note that the request content type is set to application/json.

like image 66
Amit Rai Sharma Avatar answered Oct 02 '22 19:10

Amit Rai Sharma


It is being returned in Json if you look at the format of the data you get...

key: value

or in your case

string Model = "CVS-1679"

When you view it in fiddler your seeing the raw serialization transport from one MS endpoint to the other. The serialisation & De-serialisation elements in the .NET framework take care of transporting it across the wire, so that when you get the object back into your .NET app at the calling end, you get a variable called Model with the value you expect.

If you try to send an entire class you'll see a lot of nested XML tags, but when you get the object in your code, you'll see a first class citizen in the object hierarchy.

The reason it appears in your browser is because, the browser doesn't know how to de-serialise it, and so just displays the text

like image 33
shawty Avatar answered Oct 02 '22 19:10

shawty