Unlock a world of possibilities! Login now and discover the exclusive benefits awaiting you.
Dear Qlik NPrinting experts,
My goal with this post is to know how to get the user id created by the Qlik NPrinting POST Users REST API method inside a Qlik Sense load script.
Environment:
Qlik Sense: May 2023 SR4
Qlik NPrinting: May 2023 SR1
I followed the instructions documented in the How to use Qlik NPrinting APIs inside a Qlik Sense load script post created by @Gianluca_Perin and the user creation via Qlik Sense load script implementing Qlik NPrinting REST APIs worked perfectly well. Although my new user is created as expected, I need to know why I don't get the user id returned to my Qlik Sense script.
I followed the documentation above and the table remains empty after creating the user.
Here is my Qlik Sense script and the data connection configuration is also attached:
/*** lOGIN ***/
LIB CONNECT TO 'REST_NPrinting_GET';
//Perform a GET call to NPrinting NTLM login API
RestConnectorMasterTable:
SQL SELECT
"Set-Cookie",
"__KEY__response_header"
FROM JSON "_response_header" PK "__KEY__response_header";
[_response_header]:
LOAD
[Set-Cookie] AS [Set-Cookie]
RESIDENT RestConnectorMasterTable
WHERE NOT IsNull([__KEY__response_header]);
//Extracts session cookie from the API response
// let vCookieRaw = Peek('Set-Cookie',0,'_response_header');
// let vCookie = TextBetween('$(vCookieRaw)','Secure,','Path=/',2);
let vCookieRaw = Peek('Set-Cookie',0,'_response_header');
let vCookie = TextBetween('$(vCookieRaw)', 'SameSite=None,', 'Path=/', 3);
DROP TABLE RestConnectorMasterTable;
//----------------------------------------------------------------
/*** CREATE USER ***/
LIB CONNECT TO 'REST_NPrinting_POST';
set vBody = '{"Username":"UserA","Email":"usera@qlik.com","Password":"123","Enabled":"true","Folder":"","Subfolder":"","DomainAccount":"","Timezone":"Europe/Berlin","Locale":"en"}';
let vBody = replace(vBody,'"', chr(34)&chr(34));
set vPostUserULR = 'https://mynprintingserver:4993/api/v1/users';
set vQSServer = 'https://myqliksenseserver';
RestNPPOSTandPUTTestTable:
SQL SELECT
"__KEY_data"
FROM JSON (wrap off) "data" PK "__KEY_data"
WITH CONNECTION( URL "$(vPostUserULR)", BODY "$(vBody)",
HTTPHEADER "Origin" "$(vQSServer)",
HTTPHEADER "Content-Type" "application/json",
HTTPHEADER "cookie" "$(vCookie)");
// [post_and_put_items]:
// LOAD [__KEY_data] AS [__KEY_data]
// RESIDENT RestNPPOSTandPUTTestTable
// WHERE NOT IsNull([__KEY_data]);
// DROP TABLE RestNPPOSTandPUTTestTable;
It would be really great if anyone could shed some light on this issue.
Thanks in advance.
Best regards,
Huberto Pereira Haidemann
Thoughts?
My guess is that wording used in API documentation is not ideal and does not represent what actually happens. Indeed documentation says:
"Adds a user.
Adds a new user, and returns its ID."
however what is not mentioned is that that ID is actually not part of the response and to obtain it you need to run another call.
I have nothing else to add to it - I am basing my whole limited knowledge about this around the existing documentation and what I can make from it.
@JonnyPoole - what is your take on this?
cheers
It could be an issue with the documentation. We can check. @Ruggero_Piccoli .
In the meantime, following your POST, you can use the REST Connector to issue a GET to the /users endpoint and filter on 'username' (or 'email') which are both primary keys in the user repository and values that you already have in your POST. The /users GET call *will* issue a response containing the novel user's userID value.
Hi,
The ID of the created user is returned:
Best Regards,
Ruggero
@Huberto See above comments from @Ruggero_Piccoli @Lech_Miszkiewicz @JonnyPoole
Hi @Frank_S,
Thank you @Ruggero_Piccoli, @Lech_Miszkiewicz, and @JonnyPoole for taking the time for the investigation. However, I have to say that the replies did not answer my question because I am calling the Qlik NPrinting API from the Qlik Sense script, it is a Qlik Sense/Qlik NPrinting integration that I am working on. Although the tests with Postman seemed to work as designed (the POST method returned user id), I can't say the same about the Qlik Sense script.
My question is: does the Qlik NPrinting POST Users REST API method used inside a Qlik Sense load script return the user id?
Can anyone show me an example using the Qlik Sense script? So far I haven't seen any.
Thank you once again for your time.
Regards,
Huberto
I had another look at your load script.
It appears to be incorrect (from your original post)
let vCookieRaw = Peek('Set-Cookie',0,'_response_header');
let vCookie = TextBetween('$(vCookieRaw)', 'SameSite=None,', 'Path=/', 3);
should be
Let vCookieRaw = Peek('Set-Cookie',0,'cookie_items'); Let vCookie = TextBetween(vCookieRaw,'SameSite=None,',' Path=/',SubStringCount(vCookieRaw,'SameSite=None')-1);
Although you are not getting the error in the article however you do need to update the 'let' syntax.
Also double check that your get (and post) rest connections are set up correctly to ensure end to end success.
Check in to that and let us know.
As far as providing specific code, perhaps others here can assist you with that.
(you may wish to check this community post/white page as well. It is for use 'as is' and is quite old now. If you need assistance developing your requirement, you may wish to consider working with a trusted partner or arrange a professional services engagement with Qlik Professional Services. https://community.qlik.com/t5/Qlik-NPrinting/How-to-use-Qlik-NPrinting-APIs-inside-a-Qlik-Sense-load...)
Hi @Huberto
I divided the question in two steps. The first returns the User ID so we can go ahead with the second. I'm trying to create a working example of the reload script that creates the user.
Best Regards,
Ruggero