cancel
Showing results for 
Search instead for 
Did you mean: 
JeremyFriedel
Sisense Team Member
Sisense Team Member

Using the InternalHttp Function Within Scripts and Plugins

The InternalHttp function is a Sisense function within the Sisense internal Prism object. The prism object and the InternalHttp function are present on all Sisense pages and can be used in scripts and plugins, including when embedded with various forms of Sisense embedding. It facilitates custom additional API requests to the Sisense server by applying the same request headers used for internal Sisense requests to handle details of API requests such as authentication, CORS, and CSRF.

Custom additional requests to Sisense API endpoints via custom scripts and plugins are often utilized to perform additional JAQL requests for fetching data from Sisense data models. This process is documented in this informative Sisense Community page.

The Community page code remains current and includes an example of using the InternalHttp function for additional JAQL requests. Below is a simplified and shortened version of the example code:

 

 

widget.on('ready', function (se, ev) {

    function runHTTP(jaql) {

        // Use $internalHttp service if exists

        const $internalHttp = prism.$injector.get("base.factories.internalHttp");



        const ajaxConfig = {

            url: "/api/datasources/x/jaql",

            method: "POST",

            data: JSON.stringify(jaql),

            contentType: "application/json",

            dataType: "json",

            async: false,

        };



        const httpPromise = $internalHttp(ajaxConfig, true);

        // Return response values

        return httpPromise.responseJSON.values;

    }



    const jaqlObj = {

        "datasource": {

            "title": "Sample ECommerce",

        },

        "metadata": [

            {

                "jaql": {

                    "dim": "[Category.Category]",

                }

            }

        ]

    };



    console.log('JAQL Result Values are ', runHTTP(jaqlObj));

});

 

 


While InternalHttp is commonly used for JAQL API requests, it can be utilized for any Sisense endpoint, allowing custom requests to any Sisense API, not just those related to JAQL. The JAQL API is how Sisense requests data from Sisense data sources. The flexibility offered by custom API calls significantly enhances the capabilities of scripts, enabling developers to interact with the full range of Sisense functionalities.

Notable Recent Resolved Scenarios and the InternalHttp Function

  • CSRF Header Issue: A customer who previously did not use InternalHttp had various widget scripts making custom JAQL requests via the JAQL API endpoint. After modifying the CSRF setting, these scripts failed, and not only did not work, but viewing the widget logged the user out of Sisense immediately. The issue was due to missing CSRF header parameters in the request headers. This was most easily resolved by using InternalHttp in the widget scripts, which automatically includes the required CSRF header parameters.
  • Unauthorized Errors with Sisense.js: A customer using Sisense embedded with Sisense.js on a third party domain and using WAT for authentication faced "Unauthorized" API errors for the custom widget script JAQL requests. Despite working outside of WAT, these requests failed within it. This issue is a known limitation with internalHttp. The workaround for this issue is demonstrated below:

 

 

widget.on('beforequery', function (se, ev) {

    function runHTTP(jaql) {

        // Use $internalHttp service if exists

        const $internalHttp = prism.$injector.has("base.factories.internalHttp") ? 

            prism.$injector.get("base.factories.internalHttp") : null;



        const ajaxConfig = {

            url: "/api/datasources/x/jaql",

            method: "POST",

            data: JSON.stringify(jaql),

            contentType: "application/json",

            dataType: "json",

            async: false,

            xhrFields: {

                withCredentials: true,

            },

        };



        const httpPromise = $internalHttp ? $internalHttp(ajaxConfig, false) : $.ajax(ajaxConfig);

        // Return response

        return httpPromise;

    }



    const jaqlObj = {

        "datasource": {

            "title": "Sample ECommerce",

        },

        "metadata": [

            {

                "jaql": {

                    "dim": "[Category.Category]",

                }

            }

        ]

    };



    runHTTP(jaqlObj).then((API_Response) => {

        console.log('JAQL Result Values are ', API_Response.data.values);

    });

});

​

 

 


In this workaround, the second parameter of the InternalHttp function is set to false, and the returned object is handled as a JavaScript promise.

The InternalHttp function is designed to be updated along with Sisense, and to adapt to any changes, allowing for scripts written for one environment (in the native Sisense UI for example) to work in many others without issue (when embedded in a third-party domain for example).

These scenarios highlight the value of InternalHttp and its potential applications. Leveraging this function allows developers to ensure their custom scripts remain robust and secure in all scenarios, including in environments with specific security requirements or complex authentication mechanisms. As Sisense continues to evolve and update, understanding and utilizing internal tools like internalHttp will be helpful for maximizing the platform's capabilities and maintaining seamless integration within various deployment and embedded contexts.

Rate this article:
Version history
Last update:
‎07-09-2024 12:18 PM
Updated by: