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.
Published: 2024-09-26
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 - use import:
js
import restApi from '@sitevision/api/server/RestApi';
For Script module - use require:
js
const restApi = require('RestApi');
Step 2 - Make use of RestApi
js
// 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;