Skip to end of metadata
Go to start of metadata

You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 4 Next »

Overview

Allows to call rest services with JSON or XML content types. Rest service operations consist of objects RestClient and RestRequestRestResponse.

const $Rest;

Creating Rest Client

To create a client use $Rest.Create method by providing the rest RestRequest.Url | service base url.

Example

var client = $Rest.Create('http://api.geonames.org/');

Some of rest services may require the authentication. Emakin supports the OpenAuth2 based authentication services and to create a authenticated client please specify the service and identity as below;

Example

var client = $Rest.Create('https://graph.microsoft.com/v1.0/', 'Office365', '', $WorkItem.CompletedBy).EnsureAuthenticated();

This method acquires a authentication token from service and ensures client is authenticated. If token cannot be acquired from service ,RestClient.EnsureAuthenticated, method throws a javascript exception. You can also use ,RestClient.IsAuthenticated, property and ,RestClient.Authenticate, method to perform same operation without error handling.

Creating Rest Request

After creating client connection you can create rest request and populate parameters as below;

Example

var request = client.Request('childrenJSON')
                .AddQueryParameter('username', 'demo')
                .AddQueryParameter('geonameId', id);

Every request created with resource url address to added to client base url. Request url may contain parameters to build a dynamic url address like 'sitePath' parameter below;

Example

var request = client.Request("sites/{sitePath}/drives")
                .AddUrlParameter("sitePath", sitePath)
                .AddQueryParameter("$select", "id,name");

You may also set http headers with ,RestRequest.AddHeader, method. Some rest methods requires JSON or XML based body and you can specify the content of request with ,RestRequest.AddObject, or ,RestRequest.AddXml, methods.

Fetching Rest Response

After Request populated you can call the RestRequest.Execute method to execute and fetch RestResponse object. Execute method by default does not perform any error check. If you want to be sure request has successfully executed please use RestRequest.Expect method to specify expected status code.

Example

var response = request.Execute();

if (response.StatusCode == "OK") {
    var val = response.GetHeader('X-Header');
    var obj = response.ToJson();
}

You can also use ,RestRequest.Get,, ,RestRequest.Delete,, ,RestRequest.Put,, ,RestRequest.Post,, ,RestRequest.Patch, methods to call service with corresponding method or you can manually set the ,RestRequest.Method, before executing. If service always returns JSON or XML you can use ,RestRequest.ExecuteJson, or ,RestRequest.ExecuteXml, method to fetch response as JSON object or XML navigator.

Example

var object = request.ExecuteJson();

// object.person.name;

See Also

  • No labels