Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 
ngazelle
Partner - Contributor II
Partner - Contributor II

Use a REST json response field (token) as next REST call http header (authorization token)

Hello all,

I have been struggling trying to link several standard REST calls together (LIB CONNECT TO [xxx];).

Can we get a first call response (json field) as http header for a second one?

Call 1:

LIB CONNECT TO [Call1];

[root]:
SQL SELECT
"access_token"
FROM JSON (wrap on) "root" QDL;

Call 2:

LIB CONNECT TO [NextCAll];

Labels (1)
4 Replies
Anonymous
Not applicable

Hi,

yes you can. understand that the first response also creates a datatable.

So in order to get the value of the first response, you have to PEEK the value from the datatable.

After that you take this peeked value which you put in a variable, and send it in the next request.

I do the following in my own app:

LIB CONNECT TO 'Json EndPoint';
RestConnectorMasterTable:
SQL SELECT
 "sid"
FROM XML "response";
[response]:
LOAD [sid] AS [sid]
RESIDENT RestConnectorMasterTable;
 
//read the response:
Let vSomething = peek('sid',0,'RestConnectorMasterTable');
DROP TABLE RestConnectorMasterTable;
 
LIB CONNECT TO 'EndPoint2';
RestConnectorMasterTable:
SQL SELECT
 "__KEY_results",
 (SELECT
 FROM "meta"),
 (SELECT
  (SELECT
   "attr:k" AS "k",
   "__KEY_field",
   "__FK_field_u0",
   (SELECT
    "text",
    "__FK_value"
   FROM "value" FK "__FK_value")
  FROM "field" PK "__KEY_field" FK "__FK_field_u0")
 FROM "result" PK "__KEY_result" FK "__FK_result")
FROM XML "results" PK "__KEY_results"
WITH CONNECTION(
Url "https://........:....../services/search/jobs/$(vSomething )/results"
)
;
ngazelle
Partner - Contributor II
Partner - Contributor II
Author

Hi,

The parameter I need to re-use is the query header "Authorization" "Bearer xxx".

But I am not able to remove my token from the REST editor for this call; if I remove it, I get an authentication error and cannot click on save because QLIK tests the connection before saving it.

I was expecting I could remove it from the editor so that the token is exclusively taken from the previous call.

I do not want to share my token when I share the QLIK project...

Anonymous
Not applicable

Hi,

For the part on your question to remove the token from your initial request. never done that before (i am the only Qlik developer in my company..) This is a hunch, but i think its worth a try. perhaps you can remove it via the QMC in the dataconnections. If you open your connection you can see and edit your Connection string.

To completly build your request search the community or google with the "With Connection" function, you can change everything from your request.

I only got this example in SOAP I use for you:

I first created my body and put it into a variable

Set vRequestBody = '<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:emp="https://...../...."><soapenv:Header><emp:AuthHeader><emp:Username>$(vUser)</emp:Username><emp:Token>$(vToken)</emp:Token></emp:AuthHeader></soapenv:Header><soapenv:Body><emp:GetList><emp:EmployeeId>$(vEmployee)</emp:EmployeeId><emp:Year>$(vYear)</emp:Year></emp:GetList></soapenv:Body></soapenv:Envelope>';

then i do the request like this:

SQL

SELECT
……………………...

WITH CONNECTION (
BODY "$(vRequestBody)"
);

Other topic which can help you on your Quest:

https://community.qlik.com/t5/Qlik-Sense-Integration/With-Connection-statement-What-are-all-the-para...

Hope this will help you.

ngazelle
Partner - Contributor II
Partner - Contributor II
Author

Thanks for the feedback.

But unless there i a way to save a REST connection from the editor without "Test connection" being a blocker, not much we can do.

I have the same issue with the our login call (credentials are pushed as body). If I do not put the body in th call editor, the test connection fails and I cannot save the call with the editor...

"WITH CONNECTION" seems to be working only for optional parameters.

I tried setting a CUSTOM CONNECT with no luck. It seems nobody is able to make it work, in the Net. I always get "Obsolete" error message:

 

CUSTOM CONNECT TO [Provider=QvRestConnector.exe;
timeout=30;
method=POST;
];

let vBody = '"{""login"":""Me"",""password"":""Blabla""}"';

RestConnectorMasterTable:
SQL SELECT
"access_token"
FROM JSON (wrap on) "root" QDL
WITH CONNECTION (
Url "https://xxx/api/auth/login-credentials",
BODY $(vBody),
HTTPHEADER "Content-Type" "application/json"
);