Connecting with Node.js
Node.js is a web technology that is based on the Google Chrome JavaScript engine. It has native functions for connecting to web services.
The following example shows how to use Node.js to connect to the Qlik Sense Repository Service (QRS) API and retrieve a list of apps in JSON format.
In the example, certificates are used for authentication and they must be in PEM format. The certificates can be obtained by exporting them from the Qlik Management Console (QMC) in PEM format including the secret key.
Exporting certificates (only in English)
When using a certificate to authenticate, requests are made with full administrator rights. To run the commands as a specific user, add the X-Qlik-User header to use the security context of that user account.
To run this example:
- Copy and paste the following code into a text editor.
- Update the host name and certificate paths according to your environment.
- Save the file as get_apps.js.
- Execute the code from a Node.js installation:
node get_apps.js
var https = require('https'); var fs = require('fs'); var options = { hostname: 'server_name.com', port: 4242, path: '/qrs/app?xrfkey=abcdefghijklmnop', method: 'GET', headers: { 'x-qlik-xrfkey' : 'abcdefghijklmnop', 'X-Qlik-User' : 'UserDirectory= Internal; UserId= sa_repository ' }, key: fs.readFileSync("C:\\client_key.pem"), cert: fs.readFileSync("C:\\client.pem"), ca: fs.readFileSync("C:\\root.pem") }; https.get(options, function(res) { console.log("Got response: " + res.statusCode); res.on("data", function(chunk) { console.log("BODY: " + chunk); }); }).on('error', function(e) { console.log("Got error: " + e.message); });