hub.eb?material_id=537&track_id=538

Calling REST Web Services using JavaScript REST API


Consuming a REST Web Service can be done using the methods found in the JavaScript services.rest API.

For each of the common HTTP Methods, GET, POST, PUT, DELETE, HEAD and PATCH,  there is a corresponding JavaScript Method. For example, the services.rest.get() will send a HTTP Request with a GET Method and return a response object.

var responseObject = services.rest.get("http://verjtraining.verj.cloud/apps/api/coin/flip");

The services.rest API request methods have optional arguments which include body (if applicable), request headers, request parameters, authorization and REST options.

The response object that is returned by a services.rest HTTP request has the following attributes:

  • body the response body
  • code the response status code
  • headers the response headers
  • success indicates request success

To view these attributes in your logs you will need to use the log() function. There is no debug feature when calling a REST Web Service using the services.rest API.

Steps


1

Create a form and add a button control with the text “Flip Coin”.

2

Add a server-side On click event script to the button with the following code:

var responseObject =  services.rest.get("http://verjtraining.verj.cloud/apps/api/coin/flip");

// check the first digit of the response code, a three digit number, e.g. 200
switch(responseObject.code.charAt(0)){

	case 2:
		log("Success");
		log(responseObject.body);
		break;

	case 4:
		log("Client Error");
		break;

	case 5:
		log("Server Error");
		break;

	default:
		break;
}

Current Module

Related