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
Hi @Huberto
Have you studies documentation? https://help.qlik.com/en-US/nprinting/May2023/APIs/NP+API/index.html?page=72
I am not sure what your expectations were, but based on your script it is all working as it should. POST command of creating user can give you response as documented in the link provided above. Those Responses are: 201Success. User has been created, 400 The request syntax is malformed, 403 The user is not authorized to create a new user.
There API does not return ID of the created user within that API call so everything is working as expected!
Obviously you can run subsequent API queries which can validate for you if your user was created (for example based on email address) and then you can obtain user id using API.
I have written quite broad NPrinting.qvs library which covers very large spectrum of NPrinting API in form of Qlik script subroutines. You can check it out here: https://nprintingadventures.com/2019/04/08/nprinting-api-qlik-rest-subroutines/
There you will have examples of how to get user data (subroutine NP_GetUsers).
cheers
Hello @Lech_Miszkiewicz,
Thank you for your quick reply. Yes, I have been working with the Qlik NPrinting REST API documentation.
What you saw in the post is just a piece of code for an easier analysis from a bigger context.
I also checked your page, which is by the way very rich. However, in none of the documentation, I found the answer to my question.
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.
You said my script is working as it should. Well, if you are talking about creating the user, then yes. Otherwise, my question remains open.
If the Qlik NPrinting POST Users REST API documentation says "Adds a user" and "Adds a new user, and returns its ID", why can't I get the user id of the user that was created? I see two possibilities:
1. It is not possible to get the user id of the created user via Qlik Sense script (a kind of limitation), which I personally don't believe because my sample came from the How to use Qlik NPrinting APIs inside a Qlik Sense load script post created by @Gianluca_Perin and I assume he wouldn't add the [post_and_put_items] table if nothing is returned by the POST method.
2. There is something missing in my code or my REST data connection may not be fully correct.
Did you run my script? Did it return the user id? The unique changes that need to be done are in the following lines:
set vPostUserULR = 'https://mynprintingserver:4993/api/v1/users';
set vQSServer = 'https://myqliksenseserver';
If the POST method does not return the user id to the Qlik Sense script, the GET user would be my workaround.
But before implementing that I need to make sure my script is working fine and this is a limitation of the Qlik Sense script and Qlik NPrinting POST Users REST API.
Thanks and regards,
Huberto Pereira Haidemann
Well, Where in documentation you see that POST user response returns created user ID?
Yes API POST creates ID as it is autogenerated ID which is generated when you create user however API responses are highlighted below:
So, if you are after ID of the user you will need to run subsequent GET user call.
Cheers
From the Qlik NPrinting Help, the same one you shared previously. You can see the "Adds a new user, and returns its ID" in the same image in your last post too.
So, I assume an ID should somehow be returned.
Similarly to the POST Users method, Qlik shows an example of filter creation in the link below and mentions the following:
"The response includes the ID of the newly created filter, which is returned in the location header."
In that case, they showed an example using Postman but what caught my attention was the part highlighted in bold above. Therefore, I understand that the ID is returned but it doesn't work in the Qlik Sense script or I don't know how to get it because I am not using the right JSON structure.
When we deal with the Qlik NPrinting REST APIs, the first thing we need is to authenticate and a similar script like the one below is used to retrieve the cookie. So, it seems to me that there is a way to return the ID, the response code, and the response description.
RestConnectorMasterTable_TMP:
SQL SELECT
"Content-Length",
"Cache-Control",
"Content-Type",
"Date",
"Set-Cookie",
"Server",
"__KEY__response_header",
(SELECT
"result",
"code",
"message",
"__FK_root"
FROM (wrap on) "root" FK "__FK_root")
FROM JSON "_response_header" PK "__KEY__response_header";
Thank you very much for taking the time to analyse my question.
Best regards,
Huberto Pereira Haidemann
Hi @Huberto
did you find how to get the id of a user created by a post API ?
i struggle with it without success 🙂
the workaround it to make a get request after the post one, but i do agree with you when you say "So, it seems to me that there is a way to return the ID, the response code, and the response description"
Have a good day.
Johann
Hi @jchoucq
Please see this article which will hopefully put you on the right track.
Kind regards
Hi @jchoucq,
Unfortunately, I didn't succeed in getting the response from the Qlik NPrinting REST API methods.
For now, I had to use the get method to retrieve the ID of the record created by the POST method.
Let me know if you were able to make any progress.
Thanks and regards,
Huberto
Hi @Frank_S,
It is not about any error when using the Qlik NPrinting REST APIs, all methods work fine and do what they were supposed to. However, as demonstrated in detail in the initial post, the methods do not return the ID as expected when the REST API method is called from the Qlik Sense script.
If you could take a deeper look into my question, that would be great.
Thanks and regards,
Huberto
Hi @Huberto
same for me, i use the get method also after the post request.
I think that, as you said, the id of the created object is not returned by the API when executed from Qlik.
Johann