hub.eb?material_id=617&track_id=614

JavaScript API: Gateway


Service Plans can communicate with a Gateway using Gateway REST Services.

The gateway.rest interface includes functions for calling the REST API with different HTTP methods. These include:

The gatewayId is the Name that was entered when creating the Gateway Connection.

You can find the names of your configured Gateways in the Service Plan's Server Admin App > Gateway Connections.

Steps


1

These steps follow on from Configuring a Gateway Connection on a Service Plan.

Create a Gateway REST Service on the Gateway (New > REST Services > Gateway REST Service).

2

Add an endpoint called flip and attach the following code to the Endpoint event:

// Call an API to retrieve the coin flip
var response = services.rest.get("https://verjtraining.verj.cloud/api/coin/flip");

// Return the results to the remote application
if(response.isSuccess()) {
   var results = JSON.parse(response.getBody());
   form.rest.setResponseBody(results.flip);
} 
else {
   form.rest.setResponseStatus(500);
   form.rest.setResponseBody("Something went wrong.");
}
3

Create a form on the remote application called coinFlip.

Create a Field called result and add it to the page.

4

Add a button to the page and paste the following into its On Click event:

// Use the Gateway REST Service to simulate a coin flip
var response = gateway.rest.get("myGateway", "flip");

// Display the results on the form
if(response.isSuccess()) {
   fields.result.value = response.getBody();
}
else {
   fields.result.value = "Unknown";
}

 

5

Run the coinFlip form and click on the button. Notice the result of the coin flip is displayed.

Current Module

Related