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
  • ๐Ÿ” Authentication - You must include your apiKey in the request header. for example: bf5b0500-9332-11e6-9c03-853f647fe4a4

    Note: The apiKey shown in the examples below is only a sample. Replace it with your own valid API key issued by the Formbird platform.

  • 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 โœ… Name of component Document containing the web service
functionName โœ… Name of the function being called in the web service
functionParameters โ›” (optional) 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 from ws-test-service Component Document

๐Ÿ“„ Source: ws-test-service

{
  componentName : "ws-test-service",

  getGreeting : async function(args) {
      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));
      return docs;
  },

  echoParameters : async function(args) {
      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));
      return docs;
  },

  sortParameters : async function(args) {
      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));
      return docs;
  }
}
Example Request - calling getGreeting
curl https://comp-dev-ng.formbird.com \
 -H "Content-Type: application/json;charset=UTF-8" \
 -H "apiKey: bf5b0500-9332-11e6-9c03-853f647fe4a4" \
 -d "{\"componentDocumentName\": \"ws-test-service\", \"functionName\": \"getGreeting\"}"
Example Response
{
    "greeting": "hello world"   
}
Example Request - calling echoParameters
curl https://comp-dev-ng.formbird.com \
 -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 https://comp-dev-ng.formbird.com \
 -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": 'getGreeting', // functionName
                    "componentWebServiceName": "ws-test-service",
                    "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": "getGreeting", // functionName
            "componentWebServiceName": "ws-test-service",
            "functionParameters": '' // functionParameters
          };

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