Skip to main content
Announcements
Join us at Qlik Connect for 3 magical days of learning, networking,and inspiration! REGISTER TODAY and save!

Node js: Send a ticket request (Qlik Sense Proxy API)

No ratings
cancel
Showing results for 
Search instead for 
Did you mean: 
Damien_V
Support
Support

Node js: Send a ticket request (Qlik Sense Proxy API)

Last Update:

Jul 7, 2022 3:06:37 AM

Updated By:

Damien_V

Created date:

Mar 20, 2021 2:59:32 AM

This article explains how to send a simple ticket request to Qlik Sense Proxy API to get a ticket with node.js.

  • This assumes that node.js is already installed on your machine.
  • Modules used for this example are: fs, request, express
  • Certificates in pem format must be copied to the machine that is sending the request.

Environments:

  • Qlik Sense Enterprise, 3.0 and higher

 

In order to send the request, we will use Express (a Node.js webserver module) and the following code into two files ticket.js and index.js. This is hardcoded to a specific user, to show bare-bones how this works, you will need to modify this example to take inputs from the user.

  1. Create a folder  (ex : requestTicket) and save it folder on a preferred location 
  2. Using your preferable editor, create index.js and ticket.js file  (see below )
  3. Save index.js and ticket.js in  the folder created on step 1 (ex : requestTicket)

 

/***** index.js *****/
const express = require('express');
const ticket = require('./ticket');
var app = express();

app.get('/index.html', function(req,res) {
    console.log("accessed the index.html node page");
    console.log("proxyRestUri: " + req.query.proxyRestUri);
    console.log("targetId: " + req.query.targetId);
    ticket.get_ticket_redirect(req.query.proxyRestUri, req.query.targetId, function(redirectUrl){
        console.log("Redirecting to "+redirectUrl);
        res.redirect(redirectUrl);
    });
});

app.use('/', express.static('public'));

app.listen(3000, function() {
    console.log("Listening on port 3000");
});

 

 

 

/***** ticket.js *****/
var fs          = require('fs');
var request = require('request');

//define certificate folder
var directory = "C:\\ProgramData\\Qlik\\Sense\\Repository\\Exported Certificates\\your-sense-server\\";

var get_ticket_redirect = function(proxyRestUri, targetId, callback){
    
    //set up request options
    var options = {
      uri: proxyRestUri + 'ticket?xrfkey=somerandomstring',
      headers: {'content-type': 'application/json',
      'X-Qlik-xrfkey': 'somerandomstring',
      'X-Qlik-user': 'UserDirectory=SomeDomain;UserId=SomeUser'
        },
      method: 'POST',
      body: {
      "UserDirectory": "SomeDomain",
      "UserId": "SomeUser",
      "Attributes": [],
      "TargetId": targetId
    },
    json: true,
      ca: fs.readFileSync(directory+ "root.pem"),
      key: fs.readFileSync(directory+"client_key.pem"),
      cert: fs.readFileSync(directory+"client.pem"),
      rejectUnauthorized: false
    };
    
    //send request
    request(options, function (error, response, body) {
        if(error) 
        {
            console.log('Error: '+error);
            console.log(response);
        } 
        else 
        {
            console.log("== Got a ticket ==");
            console.log("Ticket: " + response.body.Ticket);
            console.log("TargetUri: " + response.body.TargetUri);
            callback(response.body.TargetUri + "?QlikTicket=" + response.body.Ticket); // This is the redirect URL!
        }
    });
    
}

module.exports = {
    get_ticket_redirect: get_ticket_redirect
}

 


Next

  1. Move to the folder that you create above  ( C:\requestTicket>)
  2. From that command line, install  nodejs modules  fs, request, express using npm  
  3. C:\requestTicket>npm install fs request express

Finally, in order to execute above codes, run the command line:

C:\requestTicket>node index.js

 If successful, it should return something like

 

C:\Users\YourUser\YourNodeApp>node index.js
Listening on port 3000

 

 

Testing 

 if the user requested https://yourserver/external-module/hub ("external-module" being the virtual proxy prefix here), and the virtual proxy is configured to send the user to https://yourauthserver:3000/index.html

Virtual Proxy settings to redirect the request to the module:

Damien_Villaret_0-1657177275003.png

In the command line, the following will appear:

accessed the index.html node page
proxyRestUri: https://yourserver:4243/qps/external-module/
targetId: 0f0daaad-288c-4d72-ba3e-19b522df88c1
== Got a ticket ==
Ticket: rDw8esOwdMCjMhZK
TargetUri: https://yourserver/external-module/hub/
Redirecting to https://yourserver/external-module/hub/?QlikTicket=rDw8esOwdMCjMhZK


Or an error if the request fails.

Note: Debugging or writing custom code is supported by the Qlik Professional Services or Presales teams. This example is provided for demonstration purposes to explain specific scenarios. No Support or maintenance is implied or provided. Further customization is expected to be necessary and it is the responsibility of the end administrator to test and implement an appropriate implementation for their specific use case.

Note: Qlik does NOT support the 3rd party software mentioned and used in this documentation. Please use them at your own discretion and, if concerned, contact the proper IT team within your company to verify the ability to use non-Qlik related software in the environment.

Labels (1)
Comments
rammilan20
Contributor II
Contributor II

What is targetId??
and where to find it or get it.
e.g targetId: 0f0daaad-288c-4d72-ba3e-19b522df88c1 how it came in final output??

Sonja_Bauernfeind
Digital Support
Digital Support

Hello @rammilan20 

Let me look into this for you.

All the best,
Sonja 

Sonja_Bauernfeind
Digital Support
Digital Support

Hello @rammilan20 

TargetID is a parameter saving the URL you were trying to access before being redirected to the authentication page. It maps the targetId and the actual URL in memory in the Qlik Proxy service.

If you require more detailed assistance with this topic, please post about what you are attempting to accomplish in our Qlik Sense Integrations and APIs forum.

All the best,
Sonja

virilo_tejedor
Creator
Creator

There is a TargetUri that seems to be the URL you were trying to access before being redirected @Sonja_Bauernfeind 

So it must be something different.  Furthermore, targetID in the example is not an URL, it is a sort of hash: 0f0daaad-288c-4d72-ba3e-19b522df88c1

I didn't found any info regarding this targetID on the Internet.  And the powershell example is not using any TargetID param

Could someone from Qlik plz clarify?

Sonja_Bauernfeind
Digital Support
Digital Support

Hello @virilo_tejedor 

I'll take this to our subject matter experts to see what additional information we can provide.

All the best,
Sonja 

virilo_tejedor
Creator
Creator

@rammilan20 @Sonja_Bauernfeind 

Here you have the answer from @Damien_V 

> Qlik Sense will remember what URL you tried to access before getting redirected to the authentication module and store it in memory as an ID, the mapping between the actual URL and the ID is stored in the Qlik Proxy service memory and cannot be seen directly.



Sonja_Bauernfeind
Digital Support
Digital Support

Hello @virilo_tejedor 

I'm glad Damien was able to provide the answer to you!

All the best,
Sonja 

Version history
Last update:
‎2022-07-07 03:06 AM
Updated by: