Skip to content

Executing REST calls from scripts (REST.call)

You can use REST.call to execute a REST call from a script.

REST.call(url, method, headers, body);
Parameter Type Required Description
url String Yes The URL on which the REST service can be reached.
method String Yes The HTTP method that is used for creating a REST call. The values that are allowed are: GET, POST, PUT, DELETE and PATCH.
headers JSON object Yes The header of the call
body JSON object Yes The body of the call.

Example

The following method calls the Google Maps Distant Matrix API to calculate the distance between the Dam in Amsterdam and the Coolsingel in Rotterdam:

REST.call("https://maps.googleapis.com/maps/api/distancematrix/json?origins=dam,amsterdam&destinations=coolsingel,rotterdam&key=API_KEY",
    'GET',
    {"Content-Type": "application/json"}
);

The result is a JSON object with the answer of the REST-service. The above mentioned request returned:

{  "destination_addresses" : [ "Coolsingel, Rotterdam, Netherlands" ],
   "origin_addresses" : [ "Dam, Amsterdam, Netherlands" ],
   "rows" : [
      {
         "elements" : [
            {
               "distance" : {
                  "text" : "76.1 km",
                  "value" : 76140
               },
               "duration" : {
                  "text" : "1 hour 3 mins",
                  "value" : 3809
               },
               "status" : "OK"
            }
         ]
      }
   ],
   "status" : "OK"
}

You can use the result as a variable in the rest of the script, for instance:

response = JSON.parse(response);
console.log("The distance is " + response.rows[0].elements[0].distance.text);