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: https://httpbin.org/#/HTTP_Methods/get_get

$request = new RESTRequest();
$request.setURL("https://httpbin.org/get");
$request.setHeader("DNT", "1");
$request.setHeader("Accept-Encoding", "gzip, deflate, br");
$request.setHeader("Host", "httpbin.org");
$request.setHeader("Accept-Language", "en,en-US;q=0.9");
$request.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko)
Chrome/75.0.3770.142 Safari/537.36");
$request.setHeader("accept", "application/json");
$request.setHeader("Referer", "https://httpbin.org/");
$request.setHeader("Connection", "keep-alive");
$response = $request.submit("GET");
var $responseBody = $response.getBodyAsString();


POST example

URL: https://httpbin.org/#/HTTP_Methods/post_post

$request = new RESTRequest();
$request.setURL("https://httpbin.org/post");
$request.setHeader("Origin", "https://httpbin.org");
$request.setHeader("Accept-Encoding", "gzip, deflate, br");
$request.setHeader("Host", "httpbin.org");
$request.setHeader("Accept-Language", "en,en-US;q=0.9");
$request.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko)
Chrome/75.0.3770.142 Safari/537.36");
$request.setHeader("accept", "application/json");
$request.setHeader("Referer", "https://httpbin.org/");
$request.setHeader("Connection", "keep-alive");
$request.setHeader("Content-Length", "0");
$request.setHeader("DNT", "1");
$response = $request.submit("POST");
var $responseBody = $response.getBodyAsString();


info Also refer to userdata/scripts/sahitests/rest/RestAPIs.sah for more examples.