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 Pro | Sahi OS | Sahi Pro Starter | Desktop Add-On | Mobile Add-On | SAP Add-On | AI Assist Add-On |
5.1.0 | NA | NA | NA | NA | NA | NA |
Available for modes: Browser
RESTRequest()
Arguments
None |
Returns
null |
Details
RESTRequest object is used to REST requests
RESTRequest object is used to REST requests
RESTRequest.addToQueryString
Since: | Sahi Pro | Sahi OS | Sahi Pro Starter | Desktop Add-On | Mobile Add-On | SAP Add-On | AI Assist Add-On |
5.1.0 | NA | NA | NA | NA | NA | NA |
Available for modes: Browser
RESTRequest.addToQueryString($key, $value)
Arguments
$key | string | query parameter key |
$value | string | query parameter value |
Returns
null |
Details
Adds a query parameter to the request URL
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.- As query string
$request = new RESTRequest(); $request.addToQueryString("Place=Bangalore");
- 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 | Sahi OS | Sahi Pro Starter | Desktop Add-On | Mobile Add-On | SAP Add-On | AI Assist Add-On |
5.1.0 | NA | NA | NA | NA | NA | NA |
Available for modes: Browser
RESTRequest.addToBody($key, $value)
Arguments
$key | string | query parameter key |
$value | string | query parameter value |
Returns
null |
Details
Adds a query parameter to the request Body
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.- As query string
$request = new RESTRequest(); $request.addToBody("Place=Bangalore");
- 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 | Sahi OS | Sahi Pro Starter | Desktop Add-On | Mobile Add-On | SAP Add-On | AI Assist Add-On |
5.1.0 | NA | NA | NA | NA | NA | NA |
Available for modes: Browser
RESTRequest.setHeader($key, $value)
Arguments
$key | string | Header name |
$value | string | Header value |
Returns
null |
Details
Sets request header
Sets request header
$request = new RESTRequest();
$request.setHeader("Accept-Encoding", "deflate,sdch");
RESTRequest.setURL
Since: | Sahi Pro | Sahi OS | Sahi Pro Starter | Desktop Add-On | Mobile Add-On | SAP Add-On | AI Assist Add-On |
5.1.0 | NA | NA | NA | NA | NA | NA |
Available for modes: Browser
RESTRequest.setURL($url)
Arguments
$url | string | url to be navigated |
Returns
null |
Details
Sets url to be navigated
Sets url to be navigated
$request = new RESTRequest();
$request.setURL("http://sahi.co.in");
RESTRequest.setBody
Since: | Sahi Pro | Sahi OS | Sahi Pro Starter | Desktop Add-On | Mobile Add-On | SAP Add-On | AI Assist Add-On |
6.0.0 | NA | NA | NA | NA | NA | NA |
Available for modes: Browser
RESTRequest.setBody($body)
Arguments
$body | string | the body of the request. |
Returns
null |
Details
Sets body of the request. Pass a JSON string.
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 | Sahi OS | Sahi Pro Starter | Desktop Add-On | Mobile Add-On | SAP Add-On | AI Assist Add-On |
6.0.0 | NA | NA | NA | NA | NA | NA |
Available for modes: Browser
RESTRequest.setCredentials($username, $password)
Arguments
$username | string | Username for authentication |
$password | string | Password for authentication |
Returns
null |
Details
Sets authentication parameters when trying to access an authorized page. Uses the credentials provided for 401 Authentication.
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.setAuthCredentials
Since: | Sahi Pro | Sahi OS | Sahi Pro Starter | Desktop Add-On | Mobile Add-On | SAP Add-On | AI Assist Add-On |
7.0.0 | NA | NA | NA | NA | NA | NA |
Available for modes: Browser
RESTRequest.setAuthCredentials($oauthtype, $oauthvalue)
Arguments
$oauthtype | string | oauthtype for authentication |
$oauthvalue | string | oauthvalue for authentication |
Returns
null |
Details
Sets authentication parameters when trying to access an authorized page. Uses the credentials provided for Basic/oAuth1.0 Authentication.
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 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");
RESTRequest.submit
Since: | Sahi Pro | Sahi OS | Sahi Pro Starter | Desktop Add-On | Mobile Add-On | SAP Add-On | AI Assist Add-On |
5.1.0 | NA | NA | NA | NA | NA | NA |
Available for modes: Browser
RESTRequest.submit($method[, $followRedirects])
Arguments
$method | string | request method, values can be "get", "post" or "delete" |
$followRedirects | boolean optional | Set this property 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
object | RESTResponse object |
Details
Submits the request, reads response and returns a RESTResponse 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 api is called before setURL it will return a null object
RESTResponse
Since: | Sahi Pro | Sahi OS | Sahi Pro Starter | Desktop Add-On | Mobile Add-On | SAP Add-On | AI Assist Add-On |
5.1.0 | NA | NA | NA | NA | NA | NA |
Available for modes: Browser
RESTResponse()
Arguments
None |
Returns
null |
Details
RESTResponse object is used to read REST responses
RESTResponse object is used to read REST responses
RESTResponse.getResponseCode
Since: | Sahi Pro | Sahi OS | Sahi Pro Starter | Desktop Add-On | Mobile Add-On | SAP Add-On | AI Assist Add-On |
5.1.0 | NA | NA | NA | NA | NA | NA |
Available for modes: Browser
RESTResponse.getResponseCode()
Arguments
None |
Returns
string | Response code |
Details
var $s = $response.getResponseCode(); //returns response code e.g. 200, 404 etc.
RESTResponse.getHeader
Since: | Sahi Pro | Sahi OS | Sahi Pro Starter | Desktop Add-On | Mobile Add-On | SAP Add-On | AI Assist Add-On |
5.1.0 | NA | NA | NA | NA | NA | NA |
Available for modes: Browser
RESTResponse.getHeader($key)
Arguments
$key | string | Header name |
Returns
string | Value of the specified response header |
Details
var $s = $response.getHeader("Content-Length"); //returns value of content-length header
RESTResponse.getBodyAsString
Since: | Sahi Pro | Sahi OS | Sahi Pro Starter | Desktop Add-On | Mobile Add-On | SAP Add-On | AI Assist Add-On |
5.1.0 | NA | NA | NA | NA | NA | NA |
Available for modes: Browser
RESTResponse.getBodyAsString([$charSet])
Arguments
$charSet | string optional | Character set. Default is "UTF-8". |
Returns
string | Content of response body |
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 Pro | Sahi OS | Sahi Pro Starter | Desktop Add-On | Mobile Add-On | SAP Add-On | AI Assist Add-On |
5.1.0 | NA | NA | NA | NA | NA | NA |
Available for modes: Browser
RESTResponse.getBody()
Arguments
None |
Returns
array | Content of response body as byte array |
Details
var $s = $response.getBody(); //returns the content of the body
Parameter
Since: | Sahi Pro | Sahi OS | Sahi Pro Starter | Desktop Add-On | Mobile Add-On | SAP Add-On | AI Assist Add-On |
5.1.0 | NA | NA | NA | NA | NA | NA |
Available for modes: Browser
Parameter()
Arguments
None |
Returns
null |
Details
Parameter object is used to add parameters to query string and body
Parameter object is used to add parameters to query string and body
Parameter.add
Since: | Sahi Pro | Sahi OS | Sahi Pro Starter | Desktop Add-On | Mobile Add-On | SAP Add-On | AI Assist Add-On |
5.1.0 | NA | NA | NA | NA | NA | NA |
Available for modes: Browser
Parameter.add($key, $value)
Arguments
$key | string | parameter key |
$value | string | parameter value |
Returns
null |
Details
Adds a key-value pair to Parameter object
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 Pro | Sahi OS | Sahi Pro Starter | Desktop Add-On | Mobile Add-On | SAP Add-On | AI Assist Add-On |
8.0.0 | NA | NA | NA | NA | NA | NA |
Available for modes: Browser
Parameter.replace($key, $value[, $index])
Arguments
$key | string | parameter key |
$value | string | parameter value |
$index | integer optional | parameter Index |
Returns
null |
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.
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 Pro | Sahi OS | Sahi Pro Starter | Desktop Add-On | Mobile Add-On | SAP Add-On | AI Assist Add-On |
5.1.0 | NA | NA | NA | NA | NA | NA |
Available for modes: Browser
Parameter.remove($key)
Arguments
$key | string | parameter key |
Returns
null |
Details
Removes a key-value pair(if exists) from Parameter object, specified by the key
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 | Sahi OS | Sahi Pro Starter | Desktop Add-On | Mobile Add-On | SAP Add-On | AI Assist Add-On |
5.1.0 | NA | NA | NA | NA | NA | NA |
Available for modes: Browser
Parameter.getQueryString()
Arguments
None |
Returns
string | query string |
Details
Creates a query string using all the key-value pairs in the Parameter object
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();