Skip to content

Execute a Web Service Component Function

POST /api/execute

Executes a Web Service Component Function

Permission to execute Component Functions is based on standard Formbird document key principles.

Arguments
  • your apiKey, for example: bf5b0500-9332-11e6-9c03-853f647fe4a4

  • json document containing execution instructions, for example:

json { "componentDocumentName": "ws-test-service", "functionName": "sortParameters", "functionParameters": [9,5,7,2,1,8,6,4,3,0] }

value required description
componentDocumentName true Name of component Document containing the web service
functionName true Name of the function being called in the web service
functionParameters false An array of parameters to pass to the function being called

The Request examples below refer to an example ws-test-service component which provides the following functions: getGreeting, echoParameters, and sortParameters. The below snippet of code shows how these functions are implemented in the ws-test-service component.

Code Snippet taken from a ws-test-service Component Document

{
  componentName : "ws-test-service",

  getGreeting : function(args) {
      var deferred = q.defer();

      const componentName = this.componentName;
      const funcName = "getGreeting";
      var ref = "componentFunction: " + componentName + "." + funcName;
      logger.info(ref + ", args: " + JSON.stringify(args));

      var docs = {"greeting": "hello world"};
      logger.info(ref + ", response: " + JSON.stringify(docs));
      deferred.resolve(docs);

     return deferred.promise;
  },

  echoParameters : function(args) {
      var deferred = q.defer();

      const componentName = this.componentName;
      const funcName = "echoParameters";
      var ref = "componentFunction: " + componentName + "." + funcName;
      logger.info(ref + ", args: " + JSON.stringify(args));

      if (!args) {args = []}
      var docs = {"parameters": args};
      logger.info(ref + ", response: " + JSON.stringify(docs));
      deferred.resolve(docs);

     return deferred.promise;
  },

  sortParameters : function(args) {
      var deferred = q.defer();

      const componentName = this.componentName;
      const funcName = "sortParameters";
      var ref = "componentFunction: " + componentName + "." + funcName;
      logger.info(ref + ", args: " + JSON.stringify(args));

      if (args) {
        args.sort();
      } else {
        args = [];
      }
      var docs = {"parameters": args};
      logger.info(ref + ", response: " + JSON.stringify(docs));
      deferred.resolve(docs);

     return deferred.promise;
  }
}
Example Request - calling getGreeting
curl http://localhost:3000/api/execute \
 -H "Content-Type: application/json;charset=UTF-8" \
 -H "apiKey: bf5b0500-9332-11e6-9c03-853f647fe4a4" \
 -d "{\"componentDocumentName\": \"ws-helloworld\", \"functionName\": \"getGreeting\"}"
Example Response
{
    "greeting": "hello world"   
}
Example Request - calling echoParameters
curl http://localhost:3000/api/execute \
 -H "Content-Type: application/json;charset=UTF-8" \
 -H "apiKey: bf5b0500-9332-11e6-9c03-853f647fe4a4" \
 -d "{\"componentDocumentName\": \"ws-test-service\", \"functionName\": \"echoParameters\", \"functionParameters\": [9,5,7,2,1,8,6,4,3,0]}"
Example Response
{
    "parameters": [9,5,7,2,1,8,6,4,3,0]
}
Example Request - calling sortParameters
curl http://localhost:3000/api/execute \
 -H "Content-Type: application/json;charset=UTF-8" \
 -H "apiKey: bf5b0500-9332-11e6-9c03-853f647fe4a4" \
 -d "{\"componentDocumentName\": \"ws-test-service\", \"functionName\": \"sortParameters\", \"functionParameters\": [9,5,7,2,1,8,6,4,3,0]}"
Example Response
{
    "parameters": [0,1,2,3,4,5,6,7,8,9]
}

Example usage in formbird Client Side Ruleset using Jquery

ruleWebService : {
            ruleCondition : true,

            asyncAjax: function( webServiceRequest ){
                return new Promise(function(resolve, reject) {
                    $.ajax({
                        url: "api/execute",
                        type: "POST",
                        data: JSON.stringify(webServiceRequest),
                        dataType: "json",
                        contentType: "application/json; charset=utf-8",
                        beforeSend: function() {            
                        },
                        success: function(data) {
                            resolve(data) // Resolve promise and when success
                        },
                        error: function(err) {
                            reject(err) // Reject the promise and go to catch()
                        }
                    });
                });
            },

            ruleAction : async function(ntf, callback) {
                const webServiceRequest = {
                    "user": {
                        "documentId": ntf.user.documentId,
                    },
                    "functionName": functionName,
                    "componentWebServiceName": "ws-example-jsonInput",
                    "functionParameters": functionParameters
                };

                const result = await this.asyncAjax(webServiceRequest);
                console.log("Result: ", result);
                callback();
            }
        }

Example usage in formbird Server Side (non Jquery) Ruleset

...     
        asyncAxios: function( webServiceRequest ){
        return new Promise(function(resolve, reject) {
            axios({
                "api/execute",
                method: "POST",
                data: JSON.stringify(webServiceRequest),
                headers: {
                    "Accept": "application/json",
                    "Content-Type": "application/json"
                }
            })
            .then( function(data) { 
                resolve(data);
            })
            .catch(function(err){
                reject(err);
            });
        });
    },

    ruleAction : async function(ntf, callback) {
        const axios = require("axios");
        const webServiceRequest = {
            "user": {
              "documentId": ntf.user.documentId,
            },
            "functionName": "testFunction1",
            "componentWebServiceName": "ws-example-jsonInput",
            "functionParameters": functionParameters
          };

        const result = await this.asyncAxios(webServiceRequest);
        console.log("Result: ", result);
        callback();
...