How to get CSRF Token in EKL

Introduction

This page is intended to explain how to get the CSRF token of the 3DSpace, for which you want to call web services.

To call any web service, you need its CSRF token.

Example

Below is the ready to use EKL Script. You can create a knowledge action to execute this script in a PPR.


GetCSRFToken EKL


Explanation

Below is the step by step introduction of the script.

1. We need the URL of the current 3DSpace Server. The line of code given below is used to get the server URL.

strCurr3DSpaceURL= GetSystemInfo("serverurl") //Get the server URL of current session

2. Create a HTTP client which will be used to send requests to the 3DSpace

//Create HTTP Client
Set oClient=CreateHTTPClient()

3. Define the web service URL to Get CSRF token.

//Define the web sevice URL
EnoWebServiceURL = strCurr3DSpaceURL + "/resources/v1/application/CSRF"

4. Send "Get" request to the 3DSpace Server. The response is returned in the form of DataTreeNode (JSON).

//Send Get request to retrive CSRF token
sResponse=oClient.Get(EnoWebServiceURL,dtResponse)

5. Extract the received DataTreeNode (here, dtResponse) to extract CSRF Token.

//Navigate the Response to tag "value"
    drResponseList=dtResponse->Query("DataTreeNode","x.Name  == \\"value\\"")
    if (drResponseList->Size()>0)
    {
        drResponseNode=drResponseList->GetItem(1) //Get the 1st item of list
        sCSRFTokenValue=drResponseNode.String //Convert to string format
        if (bTraceAll)
        {
            Trace(1,"ENO CSRF Token :#",sCSRFTokenValue)
        }
    }

The "sCSRFTokenValue" string variable is returned to the EKL Action which calls this Action file.

How to deploy

1. Create a Knowledge Action. (Click here to know more)​​​​​​​

2. Set the input arguments in the Knowledge action as given below.

3. To call this action in another action:

    i. Create a variable of type "AdvisorAction" in the action where you want to call this action.

Let getCSRFTokenAction(AdvisorAction)

    ii. Set this variable as the path of the newly created action file.

getCSRFTokenAction = `Knowledge Engineering Specification Physical00000167 A.1\\Relations\\GetCSRFToken`

iii. Call the action by passing necessary arguments.

getCSRFTokenAction.Run(sCSRFTokenValue)

iv. The CSRF token value returned by the "GetCSRFTokenAction" will be saved in the "sCSRFTokenValue" string.

Author

person of contact - @KA ​​​​​​​