REST resource JavaScript asset

The REST resource JavaScript asset is used to communicate with web services exposing themselves using representational state transfer (REST) methods. The asset enables content to be retrieved from other REST web services, allowing the posting and modification of data using HTTP GET, POST, HEAD, PUT and DELETE requests.

Access to servers is processed through authentication settings with the REST resource OAuth session and REST resource OAuth 2-legged assets, allowing access to OAuth secure REST web services.

The Details screen of the REST resource JavaScript asset allows you to configure the settings of the HTTP request to the external web server(s) and the JavaScript processing of the response. Read the Asset screens documentation for more information about the Status, Future status, Thumbnail, and Details sections.

HTTP request

This section allows you to configure the settings of the HTTP request of the REST resource, including the request method and URL of the target REST web service.

The fields available in this section are similar to those for a REST resource asset.

JavaScript processing

This section allows you to either select a JS file or manually enter JavaScript to determine the content processed by the REST resource JavaScript asset.

Request data is available as JSON data using the pre-defined variable _REST. For example, print(_REST.response.body); will print the body of the first REST web server response.

The fields available in this section are as follows:

Session var name

The response of HTTP response array object can be stored as a custom specified variable in the matrix session sandbox based on the value you put into this field. You can overwrite this array value with a custom one if you print anything using the JavaScript field.

For example, if you enter response in this field and in the JavaScript field you enter print(_REST.response.body), the value of the _REST.response.body will be stored in the session variable, which can then be accessed using a custom session keyword of %globals_session_response%.

This field is only available on the call REST resource form submission action and the call REST resource trigger action.

Pre-process matrix global keywords

Enable this option to allow global keyword replacements (%globals_*%) to be evaluated before run JavaScript.

JS engine

Select the engine to execute JS commands on the REST resource JavaScript asset. By default, this is set to use the v8 JavaScript engine, which requires the PECL v8js extension installed on your system.

Read the PECL v8js package documentation for more information. Alternatively, you can select to use SpiderMonkey, as configured on the External tools configuration screen.

Read the External tools configuration documentation for more information.

Include files

Select a file asset, such as a JS file, within the system for JavaScript processing. To select more than one file, select Moreā€¦. An additional field will appear on the screen. Files selected in this field will be run in sequential order, followed by any code entered in the JavaScript field.

Process keywords in JavaScript files

Files added through the 'included files' field can be individually selected for keyword processing in this section.

JavaScript

Enter valid JavaScript to use for JavaScript processing. Example JavaScript is shown below.

var s = _REST.response.body;
print(s.length);
print(_REST.response.body);

When this JavaScript code is used, the character length of the REST response body is printed, followed by the body content itself.

You can also access and use response error messages and codes returned by the response, for example:

print(_REST.response.error);
print(_REST.response.error_code);

HTTP logs

Whenever an outbound request is made, HTTP logs are created and made available through the Log manager.