Can I access the REST-API from a Script or WebApp/RESTApp?

RestApi offers a way to access the REST-API server-side, i.e. without making any outgoing http request

RestApi is a util that provides methods to make server-side calls to the REST-API. It is very useful since some data is only exposed through the REST-API and not in the Public API.

Step 1 - Get an instance of RestApi

For WebApp/RESTApp 2; use import:

import restApi from '@sitevision/api/server/RestApi';

For Script module and WebApp/RESTApp 1; use require:

const restApi = require('RestApi');

Step 2 - Make use of RestApi

// In this example the restApi is used to GET the comments on the current page

// Fetch the current page
const currentPage = portletContextUtil.getCurrentPage();

// Make the "call" to the REST-API providing the current page as context node and "comments" as operation
const response = restApi.get(currentPage, 'comments');

// response is a script object representing the response (properties: statusCode, statusMessage, body)
if (response.statusCode !== 200) {
  // Handle error and return
}

// The body of the response contains the actual data, in this example an array of comments
const body = response.body;

Do you want to subscribe to News from Sitevision Developer team? Subscribe here!