When trying to call an API in NPrinting from JavaScript, like to trigger a task, the error "REVEL_CSRF: tokens mismatch" is returned. How can the NPrinting API be called successfully?
Environment:
All versions of NPrinting
The error is happening because the token is not being provided in the header with the POST request, which is a requirement with the NTLM authentication method. However, there seems to be native browser security controls that prevent access to the token found in the cookie after authentication when using JavaScript.
A GET request must first be made to authenticate. It is then required to provide the XSRF TOKEN parsed from the cookie in the header of the GET request for every POST request made, which is just a simple header entry. This requirement is documented towards the top of this article
How to Authenticate / Connect with the Qlik NPrinting API in Postman with NTLM Authentication where it says "If making a POST request, you need to also include the X-XSRF-TOKEN header with the value of the cookie, otherwise you will receive the error REVEL_CSRF: tokens mismatch," which in this case is not happening since the provided JavaScript code is probably not doing this correctly. The closest parsing example that can be found for now is located here
https://community.qlik.com/t5/Qlik-NPrinting-Discussions/Nprinting-on-demand-API-with-JQuery/m-p/139220/highlight/true#M4911, but there could be native security controls that prevent this from working without using a higher level library from what can be read further below the sample.
Another approach that can be taken, which would greatly simplify things, is to use a different authentication method. For example, the JWT method would allow someone do make the calls they need with just one request. Documentation for this method can be found here
NPrinting API: How to execute a publish task (PowerShell).