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.0Since Sahi OS: NA
RESTRequest()
Arguments
None |
Details
RESTRequest object is used to REST requests
RESTRequest object is used to REST requests
RESTRequest.addToQueryString
Since Sahi Pro: 5.1.0.0Since Sahi OS: NA
RESTRequest.addToQueryString($key, $value)
Arguments
$key | string | query parameter key |
$value | string | query parameter value |
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: 5.1.0.0Since Sahi OS: NA
RESTRequest.addToBody($key, $value)
Arguments
$key | string | query parameter key |
$value | string | query parameter value |
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: 5.1.0.0Since Sahi OS: NA
RESTRequest.setHeader($key, $value)
Arguments
$key | string | Header name |
$value | string | Header value |
Details
Sets request header
Sets request header
$request = new RESTRequest();
$request.setHeader("Accept-Encoding", "deflate,sdch");
RESTRequest.setURL
Since Sahi Pro: 5.1.0.0Since Sahi OS: NA
RESTRequest.setURL($url)
Arguments
$url | string | url to be navigated |
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: 6.0.0Since Sahi OS: NA
RESTRequest.setBody($body)
Arguments
$body | string | the body of the request. |
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: 6.0.0Since Sahi OS: NA
RESTRequest.setCredentials($username, $password)
Arguments
$username | string | Username for authentication |
$password | string | Password for authentication |
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.submit
Since Sahi Pro: 5.1.0.0Since Sahi OS: NA
RESTRequest.submit($method)
Arguments
$method | string | request method, values can be "get", "post" or "delete" |
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 objectRESTResponse
Since Sahi Pro: 5.1.0.0Since Sahi OS: NA
RESTResponse()
Arguments
None |
Details
RESTResponse object is used to read REST responses
RESTResponse object is used to read REST responses
RESTResponse.getResponseCode
Since Sahi Pro: 5.1.0.0Since Sahi OS: NA
RESTResponse.getResponseCode()
Arguments
None |
Details
Returns response code
Returns response code
var $s = $response.getResponseCode(); //returns response code e.g. 200, 404 etc.
RESTResponse.getHeader
Since Sahi Pro: 5.1.0.0Since Sahi OS: NA
RESTResponse.getHeader($key)
Arguments
$key | string | Header name |
Details
Returns response header
Returns response header
var $s = $response.getHeader("Content-Length"); //returns value of content-length header
RESTResponse.getBodyAsString
Since Sahi Pro: 5.1.0.0Since Sahi OS: NA
RESTResponse.getBodyAsString()
Arguments
None |
Details
Returns response body as string
Returns response body as string
var $s = $response.getBodyAsString(); //returns the content of the body
RESTResponse.getBody
Since Sahi Pro: 5.1.0.0Since Sahi OS: NA
RESTResponse.getBody()
Arguments
None |
Details
Returns response body as byte array.
Returns response body as byte array.
var $s = $response.getBody(); //returns the content of the body
Parameter
Since Sahi Pro: 5.1.0.0Since Sahi OS: NA
Parameter()
Arguments
None |
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: 5.1.0.0Since Sahi OS: NA
Parameter.add($key, $value)
Arguments
$key | string | parameter key |
$value | string | parameter value |
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");
Parameter.remove
Since Sahi Pro: 5.1.0.0Since Sahi OS: NA
Parameter.remove($key)
Arguments
$key | string | parameter key |
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: 5.1.0.0Since Sahi OS: NA
Parameter.getQueryString()
Arguments
None |
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();