Sahi Pro - Accessing Web Services through REST APIs


REST APIs have been introduced in Sahi Pro 5.1.0.0. These can be used to access WebServices.

Here are two examples of how GET and POST requests can be handled.

GET example
URL: http://services.aonaware.com/DictService/DictService.asmx/Define?word=potato

var $request = new RESTRequest();
$request.setHeader("Accept-Encoding", "deflate,sdch");
$request.setURL("http://services.aonaware.com/DictService/DictService.asmx/Define");
$request.addToQueryString("word", "potato");
var $response = $request.submit("get");
var $responseBody = $response.getBodyAsString();

// The response is in the form of an xml, so we will parse the xml.
// Alternatively, this can be JSON in which case, we would need to eval the JSON to get the output object attributes.
$data = $responseBody.replace(/<[?]xml[^<]*</, '<');

default xml namespace = "http://services.aonaware.com/webservices/";
var $xml = new XML($data);
var $meaning = $xml.Definitions[0].Definition[0].WordDefinition[0].toString();
_alert($meaning);

POST example

URL: http://www.ezzylearning.com/services/CountryInformationService.asmx/GetPopulationByCountry
Post Data: countryName=India

var $request = new RESTRequest();
$request.setHeader("Accept-Encoding", "deflate,sdch");
$request.setURL("http://www.ezzylearning.com/services/CountryInformationService.asmx/GetPopulationByCountry");
var $p = new Parameter();
$p.add("countryName", "India");
$request.addToBody($p);
var $response = $request.submit("post");
var $responseBody = $response.getBodyAsString();

// The response is in the form of an xml, so we will parse the xml.
// Alternatively, this can be JSON in which case, we would need to eval the JSON to get the output object attributes.
$data = $responseBody.replace(/<[?]xml[^<]*</, '<');

default xml namespace = "http://www.ezzylearning.com/services/CountryInformationService.asmx";
var $xml = new XML($data);
_alert($xml.toString());

info Also refer to userdata/scripts/demo/testRestAPIs.sah for more examples.