hub.eb?material_id=418&track_id=414

REST Services


The REST Services interface contains functions used to consume REST endpoints. The interface allows you to create and send HTTP requests with specific headers and parameters.

The available functions include:

  • get(), used to retrieve data from a web service
  • post(), used to submit data to a web service
  • put(), used to update data on a web service
  • delete(), used to delete data from a web resource

The parameters for the above functions include:

  • a destination URI for the request
  • request headers
  • request parameters
  • HTTP authentication

Steps


1

Create a new form and add a Button Control with the text Make Request. Add two Fields:

  • dice, with the Integer Field type and the Number Display type
  • myResponse, with the Character Field type and the Text Area Display type

Drag both fields onto the page as Field Controls.

2

Add an On Click event to the button with the following code:

// Define the endpoint URI
var uri = "https://verjtraining.verj.cloud/apps/api/dice/roll";

// Define the headers map
var headers = { "Accept": "application/json" };

// Define the parameters map
// This specifies the number of dice to roll from the field
var parameters = { dice: fields.dice.value };

// Send the GET request to the endpoint
var response = services.rest.get(uri, headers, parameters);

// Display the results
if (response.isSuccess()) {
  fields.myResponse.value = response.getBody();
} else {
  fields.myResponse.value = "Error calling endpoint";
}
3

Run the form, enter 1 in the dice Field Control click the button. A GET request will be made to the remote endpoint, which simulates rolling a die, and the roll value returned. Try entering different numbers of dice.

Current Module

Related