HELP!

How can I invoke a REST Web service?

Calling a Web Service from a Deployed Application

Example Code

let myResult = xpertrule.apiCall({
    url: "http://acme.inc/service",
    type: "POST",
    data: {
      recordID: 42
    },
    dataType: "json",
    timeout: 10000
});
if (myResult.ok) {
    xpertrule.message(myResult.data);
} else {
    xpertrule.terminate(myResult.error);
}

The above code makes a call to a web service (with an endpoint of http://acme.inc/service). This is a post operation and the body of the passed message is a (stringified) JSON object {recordID: 42}.

n.b. The parameters passed to the xpertrule.api method are those specified by the jQuery ajax method. Click here for the official jQuery documentation.

Legacy Code Sample

A simpler, but not as flexible, method of consuming a web service is to use the inbuilt xpertrule.urlFetch method in your JavaScript Script as shown below. In this example the result returned from the web service is parsed and assigned to an Viabl.ai Platform object called 'result'.

myurl = 'http://54.77.185.172:8001?postcode='+#postcode.val();
//xpertrule.message(myurl);
var dataStr = xpertrule.urlFetch(myurl);
obj = JSON.parse(dataStr);
#result.val(obj.valid);

Calling a Web Service from a Silent or Chat application (raw node.js implementation)

This sample code demonstrates calling a REST based web service from a silent or chat knowledge base. The example assumes a POST based service (although GET is equally straight forward) expecting and returning JSON messages. In this sample the Node.js request object for the service call. More information can be found in the official Node.js documentation.

Example Code

// sample code to call a web service from a node.js deployed knowledge base
 
var returnData = xpertrule.onceAsync(function(completeCB) {
    // !! This section of code is guaranteed to only be called once !!
 
    // Build the data to pass to the service. In this example we assume a JSON message in the post body
    var jsonObject = JSON.stringify({
        "caption" : "Post Test",
        "name" : "This is some example text"
    });
 
    // setup the correct headers to send with the post
    var postheaders = {
        "Content-Type": "application/json",
        "Content-Length": Buffer.byteLength(jsonObject, "utf8")
    };
 
    // setup the options to connect to the service
    var options = {
        host: "api.mydomain.com",
        port: 80,
        path: "/serviceName",
        method: "POST",
        headers : postheaders
    };
 
    var rawReturn = ""; // Initialize the response (raw) buffer
 
    var reqPost = http.request(options, function(res) {
        res.setEncoding("utf8");
        res.on("data", function(chunk) {
            rawReturn += chunk; // build the response message
        });
        res.on("end", function() {
            // Assuming that the response from the service is a JSON message, decode it
            returnData = JSON.parse(rawReturn);
            // This tells viabl.ai inference engine to continue
            completeCB(returnData);
        });
    });
 
    reqPost.write(jsonObject); // send the input message in the post body
    reqPost.end(); // end the request
);
 
// use the "returnData" object from here on

On This Page