Skip to content

$Rest

Allows interaction with REST services using JSON or XML content types. REST service operations involve the following objects: RestClient, RestRequest, and RestResponse.

1
const $Rest;

Remarks

A typical call has three steps:

  1. Create a client for the base URL of the service with $Rest.Create. Pass a service name to authenticate with OAuth2 credentials configured in the domain.
  2. Create a request for a resource with RestClient.Request and add parameters, headers and a body.
  3. Execute the request. RestRequest.ExecuteJson and RestRequest.ExecuteXml return the parsed body; RestRequest.Get, RestRequest.Post, RestRequest.Put, RestRequest.Patch and RestRequest.Delete return a RestResponse.

All methods except RestRequest.Execute and RestRequest.ExecuteAsync verify the response status code and throw an error for unexpected status codes. Use RestRequest.Expect to accept other status codes.

Methods ending with Async return a Promise; use .then(function (result) { ... }) to read the result.

Examples

Read JSON from a service

1
2
3
4
5
6
7
var client = $Rest.Create('https://api.example.com/');

var customer = client.Request('customers/{id}')
    .AddUrlParameter('id', $Xml.Evaluate('Customer/Id'))
    .ExecuteJson();

$Xml.SetValue('Customer/Name', customer['name']);

Call an authenticated service

1
2
3
4
5
6
// 'Office365' is a service configured in the domain settings
var drives = $Rest.Create('https://graph.microsoft.com/v1.0/', 'Office365', '', $Initiator)
    .EnsureAuthenticated()
    .Request('me/drive/root/children')
    .AddQueryParameter('$select', 'id,name')
    .ExecuteJson();

Handle the response yourself

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
var response = $Rest.Create('https://api.example.com/')
    .Request('orders/{id}')
    .AddUrlParameter('id', $Xml.Evaluate('OrderId'))
    .Execute();

if (response.StatusCode === 'OK') {
    var order = response.ToJson();
    $Xml.SetValue('OrderStatus', order['status']);
}
else {
    console.warn('Order service returned ' + response.StatusCode + ': ' + response.Content);
}

Capabilities

Client Creation