Sahi Pro - Working with REST APIs


abstract Sahi exposes RESTRequest and RESTResponse APIs to work with REST APIs

info Please refer to this article for complete examples of how to use the REST APIs.

RESTRequest

Since Sahi Pro: 5.1.0.0
Since Sahi OS: NA

RESTRequest()

Arguments
None

Details

RESTRequest object is used to REST requests

RESTRequest.addToQueryString

Since Sahi Pro: 5.1.0.0
Since Sahi OS: NA

RESTRequest.addToQueryString($key, $value)

Arguments
$keystring query parameter key
$valuestring query parameter value

Details

Adds a query parameter to the request URL
$request = new RESTRequest();
$request.addToQueryString("Place", "Bangalore");
Single argument can also be passed to this api. This can be a string which is queryString or Parameter object. Parameter object is explained below.
  1. As query string
    $request = new RESTRequest();
    $request.addToQueryString("Place=Bangalore");
  2. As Parameter object
    $request = new RESTRequest();
    var $p = new Parameter();
    $p.add("Name", "Jane");
    $p.add("Age", "25");
    $request.addToQueryString($p);

RESTRequest.addToBody

Since Sahi Pro: 5.1.0.0
Since Sahi OS: NA

RESTRequest.addToBody($key, $value)

Arguments
$keystring query parameter key
$valuestring query parameter value

Details

Adds a query parameter to the request Body
$request = new RESTRequest();
$request.addToBody("Place", "Bangalore");
Single argument can also be passed to this api. This can be a string which is queryString or Parameter object. Parameter object is explained below.
  1. As query string
    $request = new RESTRequest();
    $request.addToBody("Place=Bangalore");
  2. As Parameter object
    $request = new RESTRequest();
    var $p = new Parameter();
    $p.add("Name", "Jane");
    $p.add("Age", "25");
    $request.addToBody($p);

RESTRequest.setHeader

Since Sahi Pro: 5.1.0.0
Since Sahi OS: NA

RESTRequest.setHeader($key, $value)

Arguments
$keystring Header name
$valuestring Header value

Details

Sets request header
$request = new RESTRequest();
$request.setHeader("Accept-Encoding", "deflate,sdch");

RESTRequest.setURL

Since Sahi Pro: 5.1.0.0
Since Sahi OS: NA

RESTRequest.setURL($url)

Arguments
$urlstring url to be navigated

Details

Sets url to be navigated
$request = new RESTRequest();
$request.setURL("http://sahi.co.in");

RESTRequest.setBody

Since Sahi Pro: 6.0.0
Since Sahi OS: NA

RESTRequest.setBody($body)

Arguments
$bodystring the body of the request.

Details

Sets body of the request. Pass a JSON string.
var $json = {
    "firstName": "John",
    "lastName": "Smith",
    "age": 25,
    "address": {
        "streetAddress": "21 2nd Street",
        "city": "Mumbai"
    }
};
var $jsonString = JSON.stringify($json);

$request = new RESTRequest();
$request.setBody($jsonString);
info SOAP requests can be made by setting the Soap request xml in the body of the request through setBody.

RESTRequest.setCredentials

Since Sahi Pro: 6.0.0
Since Sahi OS: NA

RESTRequest.setCredentials($username, $password)

Arguments
$usernamestring Username for authentication
$passwordstring Password for authentication

Details

Sets authentication parameters when trying to access an authorized page. Uses the credentials provided for 401 Authentication.
$request = new RESTRequest();
$request.setCredentials("testuser", "password");

RESTRequest.submit

Since Sahi Pro: 5.1.0.0
Since Sahi OS: NA

RESTRequest.submit($method)

Arguments
$methodstring request method, values can be "get", "post" or "delete"

Details

Submits the request, reads response and returns a RESTResponse object
$request = new RESTRequest();
$request.setURL("http://sahi.co.in");
$response = $request.submit("get");
If this api is called before setURL it will return a null object

RESTResponse

Since Sahi Pro: 5.1.0.0
Since Sahi OS: NA

RESTResponse()

Arguments
None

Details

RESTResponse object is used to read REST responses

RESTResponse.getResponseCode

Since Sahi Pro: 5.1.0.0
Since Sahi OS: NA

RESTResponse.getResponseCode()

Arguments
None

Details

Returns response code
var $s = $response.getResponseCode(); //returns response code e.g. 200, 404 etc.

RESTResponse.getHeader

Since Sahi Pro: 5.1.0.0
Since Sahi OS: NA

RESTResponse.getHeader($key)

Arguments
$keystring Header name

Details

Returns response header
var $s = $response.getHeader("Content-Length"); //returns value of content-length header

RESTResponse.getBodyAsString

Since Sahi Pro: 5.1.0.0
Since Sahi OS: NA

RESTResponse.getBodyAsString()

Arguments
None

Details

Returns response body as string
var $s = $response.getBodyAsString(); //returns the content of the body

RESTResponse.getBody

Since Sahi Pro: 5.1.0.0
Since Sahi OS: NA

RESTResponse.getBody()

Arguments
None

Details

Returns response body as byte array.
var $s = $response.getBody(); //returns the content of the body

Parameter

Since Sahi Pro: 5.1.0.0
Since Sahi OS: NA

Parameter()

Arguments
None

Details

Parameter object is used to add parameters to query string and body

Parameter.add

Since Sahi Pro: 5.1.0.0
Since Sahi OS: NA

Parameter.add($key, $value)

Arguments
$keystring parameter key
$valuestring parameter value

Details

Adds a key-value pair to Parameter object
var $p = new Parameter();
$p.add("Name", "Jane");

Parameter.remove

Since Sahi Pro: 5.1.0.0
Since Sahi OS: NA

Parameter.remove($key)

Arguments
$keystring parameter key

Details

Removes a key-value pair(if exists) from Parameter object, specified by the key
var $p = new Parameter();
$p.add("Name", "Jane");
$p.add("Age", "25");
$p.remove("Name");

Parameter.getQueryString

Since Sahi Pro: 5.1.0.0
Since Sahi OS: NA

Parameter.getQueryString()

Arguments
None

Details

Creates a query string using all the key-value pairs in the Parameter object
var $p = new Parameter();
$p.add("Name", "Jane");
$p.add("Age", "25");
var $qs = $p.getQueryString();