Working with REST APIs

abstract Sahi exposes RESTRequest and RESTResponse APIs to work with REST APIs
info Refer to this article for complete examples of how to use the REST APIs.

RESTRequest

Since: Sahi ProDesktop Add-OnMobile Add-OnSAP Add-OnAI Assist Add-On
5.1.0NANANANA

Available for modes: Browser

RESTRequest()

Arguments
None

Returns
null

Sahi Pro Flowcharts Action :RESTRequest

Details

RESTRequest object is used to REST requests

RESTRequest.addToQueryString

Since: Sahi ProDesktop Add-OnMobile Add-OnSAP Add-OnAI Assist Add-On
5.1.0NANANANA

Available for modes: Browser

RESTRequest.addToQueryString($key, $value)

Arguments
$keystring query parameter key
$valuestring query parameter value

Returns
null

Sahi Pro Flowcharts Action :RESTRequest.add To Query String

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 ProDesktop Add-OnMobile Add-OnSAP Add-OnAI Assist Add-On
5.1.0NANANANA

Available for modes: Browser

RESTRequest.addToBody($key, $value)

Arguments
$keystring query parameter key
$valuestring query parameter value

Returns
null

Sahi Pro Flowcharts Action :RESTRequest.add To Body

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 ProDesktop Add-OnMobile Add-OnSAP Add-OnAI Assist Add-On
5.1.0NANANANA

Available for modes: Browser

RESTRequest.setHeader($key, $value)

Arguments
$keystring Header name
$valuestring Header value

Returns
null

Sahi Pro Flowcharts Action :RESTRequest.set Header

Details

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

RESTRequest.setURL

Since: Sahi ProDesktop Add-OnMobile Add-OnSAP Add-OnAI Assist Add-On
5.1.0NANANANA

Available for modes: Browser

RESTRequest.setURL($url)

Arguments
$urlstring url to be navigated

Returns
null

Sahi Pro Flowcharts Action :RESTRequest.set URL

Details

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

RESTRequest.setBody

Since: Sahi ProDesktop Add-OnMobile Add-OnSAP Add-OnAI Assist Add-On
6.0.0NANANANA

Available for modes: Browser

RESTRequest.setBody($body)

Arguments
$bodystring the body of the request.

Returns
null

Sahi Pro Flowcharts Action :RESTRequest.set Body

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 ProDesktop Add-OnMobile Add-OnSAP Add-OnAI Assist Add-On
6.0.0NANANANA

Available for modes: Browser

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

Arguments
$usernamestring Username for authentication
$passwordstring Password for authentication
$isEncryptedboolean optionalSet as true if the password is encrypted

Returns
null

Sahi Pro Flowcharts Action :RESTRequest.set Credentials

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

// Use encrypted password
$request = new RESTRequest();
$request.setCredentials("testuser", "MgkKEQBU", true);
info
  • For encrypting the password use _encrypt or use password encoder from editor.
  • Salt is used to encrypt the password. You can change the salt by changing the property script.salt_for_password_encryption in userdata/config/userdata.propertiesfile.
  • If script.salt_for_password_encryption property is present in userdata/config/userdata_hidden.properties as well, then this property will be considered only from userdata_hidden.propertiesfile.

RESTRequest.setAuthCredentials

Since: Sahi ProDesktop Add-OnMobile Add-OnSAP Add-OnAI Assist Add-On
7.0.0NANANANA

Available for modes: Browser

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

Arguments
$oauthtypestring oauthtype for authentication
$oauthvaluestring oauthvalue for authentication
$isEncryptedboolean optionalSet as true if the oauthvalue is encrypted

Returns
null

Sahi Pro Flowcharts Action :RESTRequest.set Auth Credentials

Details

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");
info
  • For encrypting the password use _encrypt or use password encoder from editor.
  • Salt is used to encrypt the password. You can change the salt by changing the property script.salt_for_password_encryption in userdata/config/userdata.propertiesfile.
  • If script.salt_for_password_encryption property is present in userdata/config/userdata_hidden.properties as well, then this property will be considered only from userdata_hidden.propertiesfile.

RESTRequest.submit

Since: Sahi ProDesktop Add-OnMobile Add-OnSAP Add-OnAI Assist Add-On
5.1.0NANANANA

Available for modes: Browser

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

Arguments
$methodstring request method, values can be "get", "post" or "delete"
$followRedirectsboolean optionalSet 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

Sahi Pro Flowcharts Action :RESTRequest.submit

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 ProDesktop Add-OnMobile Add-OnSAP Add-OnAI Assist Add-On
5.1.0NANANANA

Available for modes: Browser

RESTResponse()

Arguments
None

Returns
null

Sahi Pro Flowcharts Action :RESTResponse

Details

RESTResponse object is used to read REST responses

RESTResponse.getResponseCode

Since: Sahi ProDesktop Add-OnMobile Add-OnSAP Add-OnAI Assist Add-On
5.1.0NANANANA

Available for modes: Browser

RESTResponse.getResponseCode()

Arguments
None

Returns
stringResponse code

Sahi Pro Flowcharts Action :RESTResponse.get Response Code

Details

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

RESTResponse.getHeader

Since: Sahi ProDesktop Add-OnMobile Add-OnSAP Add-OnAI Assist Add-On
5.1.0NANANANA

Available for modes: Browser

RESTResponse.getHeader($key)

Arguments
$keystring Header name

Returns
stringValue of the specified response header

Sahi Pro Flowcharts Action :RESTResponse.get Header

Details

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

RESTResponse.getBodyAsString

Since: Sahi ProDesktop Add-OnMobile Add-OnSAP Add-OnAI Assist Add-On
5.1.0NANANANA

Available for modes: Browser

RESTResponse.getBodyAsString([$charSet])

Arguments
$charSetstring optional Character set. Default is "UTF-8".

Returns
stringContent of response body

Sahi Pro Flowcharts Action :RESTResponse.get Body As String

Details

    infoNOTE: $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

RESTResponse.getBody

Since: Sahi ProDesktop Add-OnMobile Add-OnSAP Add-OnAI Assist Add-On
5.1.0NANANANA

Available for modes: Browser

RESTResponse.getBody()

Arguments
None

Returns
arrayContent of response body as byte array

Sahi Pro Flowcharts Action :RESTResponse.get Body

Details

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

Parameter

Since: Sahi ProDesktop Add-OnMobile Add-OnSAP Add-OnAI Assist Add-On
5.1.0NANANANA

Available for modes: Browser

Parameter()

Arguments
None

Returns
null

Sahi Pro Flowcharts Action :Parameter

Details

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

Parameter.add

Since: Sahi ProDesktop Add-OnMobile Add-OnSAP Add-OnAI Assist Add-On
5.1.0NANANANA

Available for modes: Browser

Parameter.add($key, $value)

Arguments
$keystring parameter key
$valuestring parameter value

Returns
null

Sahi Pro Flowcharts Action :Parameter.add

Details

Adds a key-value pair to Parameter object
var $p = new Parameter();
$p.add("Name", "Jane");
info Note: Calling this method again with the same parameter name will add one more key-pair with the value specified.
var $p = new Parameter();
$p.add("Name", "Jane");
$p.add("Name", "Doe");
$p.getQueryString(); // returns Name=Jane&Name=Doe

Parameter.replace

Since: Sahi ProDesktop Add-OnMobile Add-OnSAP Add-OnAI Assist Add-On
8.0.0NANANANA

Available for modes: Browser

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

Arguments
$keystring parameter key
$valuestring parameter value
$indexinteger optional parameter Index

Returns
null

Sahi Pro Flowcharts Action :Parameter.replace

Details

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

Parameter.remove

Since: Sahi ProDesktop Add-OnMobile Add-OnSAP Add-OnAI Assist Add-On
5.1.0NANANANA

Available for modes: Browser

Parameter.remove($key)

Arguments
$keystring parameter key

Returns
null

Sahi Pro Flowcharts Action :Parameter.remove

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 ProDesktop Add-OnMobile Add-OnSAP Add-OnAI Assist Add-On
5.1.0NANANANA

Available for modes: Browser

Parameter.getQueryString()

Arguments
None

Returns
stringquery string

Sahi Pro Flowcharts Action :Parameter.get Query String

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