Skip to main content

Connectivity & Data Prep

Discussion board where members can learn more about Qlik Sense Data Connectivity.

Announcements
QlikWorld 2023, a live, in-person thrill ride. April 17 - 20, 2023, in Las Vegas! REGISTER TODAY
cancel
Showing results for 
Search instead for 
Did you mean: 
edwinc
Partner - Contributor
Partner - Contributor

Need help on how to dynamically assign Cookie header using REST API connector in Qlik Sense

Hello Qlik Community,

I'm in need of some guidance on how to pass REST API authentication cookie dynamically for my API GET calls. I have gone through all the posts and articles related to setting cookie for REST connector but I'm not able to make it work so I'm hoping that you would be able to provide the solution or point me in the right direction.

Here's what I'm currently doing

- Using Postman, I'm doing a POST call to the REST API of the application to fetch the cookie

- Next, using Qlik REST API connector, I'm setting it up for a GET call and putting the Cookie information in the query header

- After testing the connection, I'm able to successfully load the data into Qlik app

- I tried adding the WITH CONNECTION syntax that was mentioned in some of the related posts but keep getting the HTTP protocol error 401 (Unauthorized): Requested resource requires authentication

My goal is to do multiple GET calls to fetch data from the third-party application but I don't want to create REST Connection in Qlik for each of them with a pre-defined session cookie as the cookie will expire after some time and it's not feasible to keep updating the cookie in the REST connectors every time I want to load data into my app as I want to set up a task for the app reload so want the session cookie to be dynamically set in my load script.

Below is my current load script that was generated by Qlik when I loaded the data through the REST Connection I created as mentioned in the initial steps. The script works if I remove the WITH CONNECTION as it directly gets the Cookie information from the REST Connector where I had entered it manually. I'm looking for a way to not depend on this REST Connector and use Qlik load script to get the session cookie and then use it as query headers for the subsequent GET calls. Please indicate where I'm going wrong and what I need to do to make this app work to dynamically generate the session cookie for authentication for the GET calls

Set dataManagerTables = '','records','availability','ips','groups','wifi','nodetonode','realservice','continuousRealservice';
//This block renames script tables from non generated section which conflict with the names of managed tables

For each name in $(dataManagerTables)
Let index = 0;
Let currentName = name;
Let tableNumber = TableNumber(name);
Let matches = 0;
Do while not IsNull(tableNumber) or (index > 0 and matches > 0)
index = index + 1;
currentName = name & '-' & index;
tableNumber = TableNumber(currentName)
matches = Match('$(currentName)', $(dataManagerTables));
Loop
If index > 0 then
Rename Table '$(name)' to '$(currentName)';
EndIf;
Next;
Set dataManagerTables = ;


Unqualify *;

LIB CONNECT TO [<Name of the REST Connection created>];

[records]:
LOAD
[probeId],
[uid],
[os],
[name] AS [name_u0],
[testIp],
[managementIp],
[active],
[type],
[mode],
[n2nMode],
[rsMode],
[continuousRsMode],
[typeName],
[serialNumber],
[probeGroup],
[location],
[latitude],
[longitude],
[endpointVersion],
[xrVersion],
[defaultInterface],
[defaultGateway],
[availableForMesh],
Timestamp([lastRestart] ) AS [lastRestart],
[__KEY_records] AS [-_KEY_records],
[__FK_records] AS [__KEY_root],
GeoMakePoint([latitude], [longitude]) AS [longitude_latitude];
SQL SELECT
(SELECT
"probeId",
"uid",
"os",
"name",
"testIp",
"managementIp",
"active",
"type",
"mode",
"n2nMode",
"rsMode",
"continuousRsMode",
"typeName",
"serialNumber",
"probeGroup",
"location",
"latitude",
"longitude",
"endpointVersion",
"xrVersion",
"defaultInterface",
"defaultGateway",
"availableForMesh",
"lastRestart",
"__KEY_records",
"__FK_records"
FROM "records" PK "__KEY_records" FK "__FK_records")
FROM JSON (wrap on) "root" PK "__KEY_root" QDL
WITH CONNECTION (
URL "<URL name>/api/probes",
HTTPHEADER "Cookie" "$(vCookie)"
);

[availability]:
LOAD
[from],
[to],
[mesh],
[__FK_availability] AS [-_KEY_records];
SQL SELECT
(SELECT
(SELECT
"from",
"to",
"mesh",
"__FK_availability"
FROM "availability" FK "__FK_availability")
FROM "records" PK "__KEY_records" FK "__FK_records")
FROM JSON (wrap on) "root" PK "__KEY_root" QDL;

Labels (1)
0 Replies