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://www.webservicex.net/geoipservice.asmx/GetGeoIP?IPAddress=72.14.228.129
var $request = new RESTRequest();
$request.setURL("http://www.webservicex.net/geoipservice.asmx/GetGeoIP");
$request.addToQueryString("IPAddress", "72.14.228.129");
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[^>]*\?>/, "").replace(/(\r\n|\n|\r)/gm,"");
default xml namespace = "http://www.webservicex.net/";
var $x = new XML($data);
var $CountryCode= $x..CountryCode[0].toString();
_alert($CountryCode);
POST example
URL: http://www.webservicex.net/geoipservice.asmx/GetGeoIP
Post Data: IPAddress=72.14.228.129
var $request = new RESTRequest();
$request.setURL("http://www.webservicex.net/geoipservice.asmx/GetGeoIP");
$request.setHeader("Content-Type", "application/x-www-form-urlencoded");
$request.setBody("IPAddress=72.14.228.129");
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[^>]*\?>/, "").replace(/(\r\n|\n|\r)/gm,"");
default xml namespace = "http://www.webservicex.net/";
var $x = new XML($data);
var $CountryCode= $x..CountryCode[0].toString();
_alert($CountryCode);
info
Also refer to
userdata/scripts/demo/testRestAPIs.sah
for more examples.