Skip to content
Get Free Trial

REST API Functions

Sahi exposes RESTRequest and RESTResponse APIs to work with REST APIs

Since
Sahi Pro 5.1.0

Modes Browser

RESTRequest()

Arguments

NameTypeDescription
None

Returns

null

RESTRequest object is used to REST requests

Since
Sahi Pro 5.1.0

Modes Browser

RESTRequest.addToQueryString($key, $value)

Arguments

NameTypeDescription
$keystringquery parameter key
$valuestringquery parameter value

Returns

null

Adds a query parameter to the request URL

$request = new RESTRequest();
$request.addToQueryString("Place", "Bangalore");

Single argument can also be passed to this function. 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);
Since
Sahi Pro 5.1.0

Modes Browser

RESTRequest.addToBody($key, $value)

Arguments

NameTypeDescription
$keystringquery parameter key
$valuestringquery parameter value

Returns

null

Adds a query parameter to the request Body

$request = new RESTRequest();
$request.addToBody("Place", "Bangalore");

Single argument can also be passed to this function. 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);
Since
Sahi Pro 5.1.0

Modes Browser

RESTRequest.setHeader($key, $value)

Arguments

NameTypeDescription
$keystringHeader name
$valuestringHeader value

Returns

null

Sets request header

$request = new RESTRequest();
$request.setHeader("Accept-Encoding", "deflate,sdch");
Since
Sahi Pro 5.1.0

Modes Browser

RESTRequest.setURL($url)

Arguments

NameTypeDescription
$urlstringurl to be navigated

Returns

null

Sets url to be navigated

$request = new RESTRequest();
$request.setURL("http://sahi.co.in");
Since
Sahi Pro 6.0.0

Modes Browser

RESTRequest.setBody($body)

Arguments

NameTypeDescription
$bodystringthe body of the request.

Returns

null

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);
Since
Sahi Pro 6.0.0

Modes Browser

RESTRequest.setCredentials($username, $password[, $isEncrypted])

Arguments

NameTypeDescription
$usernamestringUsername for authentication
$passwordstringPassword for authentication
$isEncryptedbooleanoptionalSet as true if the password is encrypted

Returns

null

Sets authentication parameters when trying to access an authorized page. Uses the credentials provided for 401 Authentication.

$request = new RESTRequest();
$request.setCredentials("testuser", "password");
// Use encrypted password
$request = new RESTRequest();
$request.setCredentials("testuser", "MgkKEQBU", true);
Since
Sahi Pro 7.0.0

Modes Browser

RESTRequest.setAuthCredentials($oauthtype, $oauthvalue[, $isEncrypted])

Arguments

NameTypeDescription
$oauthtypestringoauthtype for authentication
$oauthvaluestringoauthvalue for authentication
$isEncryptedbooleanoptionalSet as true if the oauthvalue is encrypted

Returns

null

Sets authentication parameters when trying to access an authorized page. Uses the credentials provided for Basic/oAuth1.0 Authentication.

// For Basic auth
$request = new RESTRequest();
$request.setAuthCredentials("authType", "Basic Auth");
$request.setAuthCredentials("userName", "testuser");
$request.setAuthCredentials("passWord", "password");
// For Basic auth with encrypted password
$request = new RESTRequest();
$request.setAuthCredentials("authType", "Basic Auth");
$request.setAuthCredentials("userName", "testuser");
$request.setAuthCredentials("passWord", "MgkKEQBU", true);
// For oAuth1.0
$request = new RESTRequest();
$request.setAuthCredentials("authType", "OAuth 1.0");
$request.setAuthCredentials("oauth_consumer_key", "consumer key");
$request.setAuthCredentials("oauth_consumer_secret", "consumer secret");
$request.setAuthCredentials("oauth_token", "token");
$request.setAuthCredentials("oauth_token_secret", "token secret");
$request.setAuthCredentials("oauth_signature_method", "HMAC-SHA1");
$request.setAuthCredentials("oauth_version", "1.0");
Since
Sahi Pro 5.1.0

Modes Browser

RESTRequest.submit($method[, $followRedirects])

Arguments

NameTypeDescription
$methodstringrequest method, values can be "get", "post" or "delete"
$followRedirectsbooleanoptionalSet this argument to false if the request should not automatically follow redirects. The default value is true.
This argument is available since Sahi Pro v10.0.0.

Returns

objectRESTResponse object

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 function is called before setURL it will return a null object

Since
Sahi Pro 5.1.0

Modes Browser

RESTResponse()

Arguments

NameTypeDescription
None

Returns

null

RESTResponse object is used to read REST responses

Since
Sahi Pro 5.1.0

Modes Browser

RESTResponse.getResponseCode()

Arguments

NameTypeDescription
None

Returns

stringResponse code
var $s = $response.getResponseCode(); //returns response code e.g. 200, 404 etc.
Since
Sahi Pro 5.1.0

Modes Browser

RESTResponse.getHeader($key)

Arguments

NameTypeDescription
$keystringHeader name

Returns

stringValue of the specified response header
var $s = $response.getHeader("Content-Length"); //returns value of content-length header
Since
Sahi Pro 5.1.0

Modes Browser

RESTResponse.getBodyAsString([$charSet])

Arguments

NameTypeDescription
$charSetstringoptionalCharacter set. Default is "UTF-8".

Returns

stringContent of response body
  • NOTE: $charSet was added Since Sahi Pro: 6.2.1. For old document Refer here

var $s = $response.getBodyAsString(); //returns the content of the body using UTF-8 encoding
var $s = $response.getBodyAsString("ISO-8859-1"); //returns the content of the body using ISO-8859-1 encoding
Since
Sahi Pro 5.1.0

Modes Browser

RESTResponse.getBody()

Arguments

NameTypeDescription
None

Returns

arrayContent of response body as byte array
var $s = $response.getBody(); //returns the content of the body
Since
Sahi Pro 5.1.0

Modes Browser

Parameter()

Arguments

NameTypeDescription
None

Returns

null

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

Since
Sahi Pro 5.1.0

Modes Browser

Parameter.add($key, $value)

Arguments

NameTypeDescription
$keystringparameter key
$valuestringparameter value

Returns

null

Adds a key-value pair to Parameter object

var $p = new Parameter();
$p.add("Name", "Jane");
Since
Sahi Pro 8.0.0

Modes Browser

Parameter.replace($key, $value[, $index])

Arguments

NameTypeDescription
$keystringparameter key
$valuestringparameter value
$indexintegeroptionalparameter Index

Returns

null

Replace a key-value pair at a specified index in the Parameter object. If the index is not specified the all the values added previously will be replaced by the value specified.

var $p = new Parameter();
$p.add("Name", "Jane");
$p.add("Name", "Woo");
$p.replace("Name", "Doe", 1);
$p.getQueryString(); // returns Name=Jane&Name=Doe
Since
Sahi Pro 5.1.0

Modes Browser

Parameter.remove($key)

Arguments

NameTypeDescription
$keystringparameter key

Returns

null

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");
Since
Sahi Pro 5.1.0

Modes Browser

Parameter.getQueryString()

Arguments

NameTypeDescription
None

Returns

stringquery string

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();