Top Navigation

Consuming JSON or non XML webservice responses

Dear WSO2 Team!

We are currently evaluating the Mashup Server and your solutions seems to be very sophisticated. We are currently implementing some test javascript services based on WSRequest and with XML-based response data everything works fine.

But we consume several services with are not based on SOAP, REST, whatever but simply return textual output (CSV, JSON,...). How can these NON-XML responses be used and accessed? It seems that every response is handled also by the Axis XML libary and this libary - of course - can not handle non XML data.

Is there a solution for this case?

Best regards

Dietmar Rietsch, elements.at

Using JSON

You can also set the option "HTTPInputSerialization" if you prefer to use JSON for message exchange.

options["HTTPInputSerialization"] = "application/json/badgerfish";

See the following blog post http://www.keith-chapman.org/2008/09/invoking-mashups-using-json.html.

regards,
Ruchira

Thanks. I've already tried

Thanks. I've already tried this, too. But it seems that the Mashup Server just can't process a JSON or non XML responses.

The error is:

Fault: org.apache.axis2.AxisFault: com.ctc.wstx.exc.WstxUnexpectedCharException: Unexpected character '{' (code 123) in prolog; expected '<' at [row,col {unknown-source}]: [1,1]

BR
Dietmar

We have just published an

We have just published an article on extending Javascript Host Object at http://wso2.org/library/tutorials/writing-custom-hostobject. This describes how to get a simpl HTTP Client integrated as a Host Object. We are planning to include this Host Object in the next release. But in the mean time, you can use the sample code here to do simple HTTP calls.

Hope this helps,

Tyrell

 

Hi, You can use the

Hi,

You can use the WSRequest Host Object to retrieve non XML responses. For instance, this code snippet is from the Yahoo GeoCode sample. Here we do an HTTP GET call to the Yahoo service by explicitly setting SOAP to false. Although we send a request payload to the url in this instance, it can also be set to 'null'.

var yahoo = new WSRequest();
var options = new Array();
options["useSOAP"] = "false";
options["HTTPMethod"] = "get";

yahoo.open(options, "http://local.yahooapis.com/MapsService/V1/geocode", false);
var request = "" + key + "" + street + "" + city + "" + state + "";
yahoo.send(request);

var response = yahoo.responseText;

Hope this helps,
Tyrell

Hi Tyrell! Thanks for the

Hi Tyrell!

Thanks for the quick response. But actually this was clear to me, because the Yahoo Service returns a valid XML/REST response.

What I would like to archieve is actually like:

var service = new WSRequest();
var options = new Array();
options["useSOAP"] = "false";
options["HTTPMethod"] = "get";

// http://clients.elements.at/wso2/service.txt contains a valid textual response in JSON format

service.open(options, "http://clients.elements.at/wso2/service.txt", false);
var request = null;
service.send(request);

// The response should now actually contain the content of the JSON response

var response = service.responseText;

If I test that service the response is:
Fault: org.apache.axis2.AxisFault: com.ctc.wstx.exc.WstxUnexpectedCharException: Unexpected character '{' (code 123) in prolog; expected '<' at [row,col {unknown-source}]: [1,1]

I therefore guess that every response is parsed by Axis and this JSON response does not - because it's JSON - start with a < character, but with a "{", representing a JSON object.

Please help ;-)