Here is how you can use JSON to invoke a simple Mashup. In this example I will be invoking the RESTSample running on mooshup.com. This simple sample sends the payload as JSON and receives the response as JSON as well.
// Providing optional annotations
invokePOSTWeather.inputTypes={"city" : "string", "weatherDetails" : "string"};
invokePOSTWeather.outputType="#raw";
function invokePOSTWeather(city, weatherDetails){
var request = new WSRequest();
var options = new Array();
// Instracts the server that my request needs to be formatted as JSON
options.HTTPInputSerialization = "application/json/badgerfish";
var payload = <POSTWeather><city>{city}</city><weatherDetails>{weatherDetails}
</weatherDetails></POSTWeather>;
var result;
try {
request.open(options,"http://mooshup.com/services/samples/RESTSample/", false);
request.send(payload);
result = request.responseE4X;
} catch (e) {
result = e.toString();
}
return result
}
If you were to use TCPMonitor to trace the messages you will be able to see the messages sent and received by the Server.
Here is the request sent out by this Mashup
{"POSTWeather":{"city":{"$":"col"},"weatherDetails":{"$":"10"}}}
And the response it received
{"ws:POSTWeatherResponse":{"@xmlns":{"ws":"http:\/\/services.mashup.wso2.org\/RESTSample?xsd"},"return":{"$":"col"}}}
Note : There are three main ways that you can get the response from WSRequest. There are response.responseText, response.responseXML and response.responseE4X. If you were to use these variants when the response if JSON they will act as follows,
response.responseText - Would return the JSON string as the response
response.responseXML - Would return the XML representation of the JSON string
response.responseE4X - Would return the E4X representation of the JSON string
3 comments:
Hi ,
I am newbie to wso2 mashup server ,I exactly followed your blog's mashup (did copy & paste) but I m not getting the json result wht possiblities it may have ?
Results sometimes coming as error like this
JavaException: org.wso2.mashup.MashupFault: The MessageContext does not have an associated SOAPFault.
where mashup is this
// Providing optional annotations
invokePOSTWeather.inputTypes={"city" : "string", "weatherDetails" : "string"};
invokePOSTWeather.outputType="#raw";
function invokePOSTWeather(city, weatherDetails){
var request = new WSRequest();
var options = new Array();
// Instracts the server that my request needs to be formatted as JSON
options.HTTPInputSerialization = "application/json/badgerfish";
var payload = "its same like ur tags as I cant post them thats why remove them ";
var result;
try {
request.open(options,"http://mooshup.com/services/samples/RESTSample/", false);
request.send(payload);
result = request.responseE4X;
} catch (e) {
result = e.toString();
}
return result
}
Sajid
looking for ur reply ,
Post a Comment