Skip to main content
Announcements
Defect acknowledgement with Nprinting Engine May 2022 SR2, please READ HERE
cancel
Showing results for 
Search instead for 
Did you mean: 
marcello
Partner - Contributor III
Partner - Contributor III

Nprinting on demand API with JQuery

Hello, I am trying to write some simple code to launch a report with a specified id.

According to the documentation i tried to get the on demand request for the given id as follow:

$.ajax({

      /* start login (this section works fine)*/

type: 'GET',

url: $('#NprintingServerURL').val()+'/api/v1/login/ntlm',

dataType: "json",

xhrFields: {

      withCredentials: true

},

crossDomain: true,

contentType :  'application/json',

success: function(data) {

      console.log(data);

},

error: function() {

      alert('Error in login');

}

/* end login (this section works fine)*/

}).done(function(data){

      var pData= {

type: 'report',

config: {

    reportId: repId,

    outputFormat: 'PDF'

}};

var idata=JSON.stringify(pData);

      $.ajax({

type: 'POST',

url: $('#NprintingServerURL').val()+'/api/v1/ondemand/requests',

data: idata,

xhrFields: {

withCredentials: true

} ,

crossDomain: true,

contentType:  'application/json',

success: function(data) {

console.log(data);  //always get an error as explained dbelow

},

error: function(data) {

alert('Error in ondemand request');

console.log(data);

}  

})



But i always get an error in ondemand request, here the response text:

"<!DOCTYPE html>

<html lang="en">

<head>

<title>Forbidden</title>

</head>

<body>

<h1>

Forbidden

</h1>

<p>

REVEL_CSRF: tokens mismatch.

</p>

</body>

</html>

"

I checked the single calls with postman and they worked.

I saw the Phyton sample, but i can't replicate it in JQuery.

I think i miss to set the header X-XSRF-TOKEN but i don't have any idea how to get the cookie values in JQuery and how to set in my code the X-XSRF-TOKEN value.

I have tried in several ways without success and it's driving me crazy, please help me.

Thanks in advance

Marcello !

4 Replies
Ruggero_Piccoli
Support
Support

Hi,

I asked the help of a developer who suggested to set cookies and X-XSRF-TOKEN token in the header to the post call as visible in the following code:

function getXSRFToken () {

var cookies = document.cookie.split( ';' ),

token = '';

cookies.forEach( function ( cookie ) {

var index = cookie.indexOf( '=' ),

key = cookie.substring( 0, index ),

val = cookie.substring( index + 1 );

if ( key.trim().toUpperCase() === 'NPWEBCONSOLE_XSRF-TOKEN' ) {

token = val;

}

} );

return token;

}

function getHeaders () {

var objHeaders = {

'Accept': 'application/json'

},

csrf = getXSRFToken();

if ( csrf.length ) {

objHeaders = $.extend( objHeaders, {

'X-XSRF-TOKEN': csrf

} );

}

return objHeaders;

}

function callback () {

var pData = {

type: 'report',

config: {

reportId: repId,

outputFormat: 'PDF'

}

};

var idata = JSON.stringify( pData ),

headers = getHeaders();

$.ajax( {

method: 'POST',

url: $( '#NprintingServerURL' ).val() + '/api/v1/ondemand/requests',

data: idata,

xhrFields: {

withCredentials: true

},

crossDomain: true,

headers: headers,

dataType: 'json',

contentType: 'application/json; charset=utf-8',

success: function ( result ) {

console.log( result );

},

error: function ( error ) {

alert( 'Error in ondemand request' );

console.log( error );

}

} );

}

Best Regards,

Ruggero

---------------------------------------------

When applicable please mark the appropriate replies as CORRECT https://community.qlik.com/docs/DOC-14806. This will help community members and Qlik Employees know which discussions have already been addressed and have a possible known solution. Please mark threads as HELPFUL if the provided solution is helpful to the problem, but does not necessarily solve the indicated problem. You can mark multiple threads as HELPFUL if you feel additional info is useful to others.



Best Regards,
Ruggero
---------------------------------------------
When applicable please mark the appropriate replies as CORRECT. This will help community members and Qlik Employees know which discussions have already been addressed and have a possible known solution. Please mark threads with a LIKE if the provided solution is helpful to the problem, but does not necessarily solve the indicated problem. You can mark multiple threads with LIKEs if you feel additional info is useful to others.
marcello
Partner - Contributor III
Partner - Contributor III
Author

Ruggero, many thanks for your accurate answer, but unforunatly it does not solve the problem.

The document.cookie always return an empty string.

I read that it's cause the browser it's not allowed to read it.

So i do not know how to deal with this, according to the documentation.

In other words, how can Postman do that ?

I mean, i've tried to reply the required steps (so i setted the proper headers value X-XSRF-TOKEN reading from cookie showed) using Postman with success, but i cannot realize how to do the same in a Javascript code.

I know cookies are keeped correctly but i cannot set the proper header value  X-XSRF-TOKEN without reading it.

Cheers, Marcello

Ruggero_Piccoli
Support
Support

Hi,

Yes, it could be a browser problem, that is out of our control. You should try a different solution that avoid to insert the code in the HTML page.

Postman is not a browser so I think it has different security policies.

Another suggestion from the developers:

The response of the login call conains the header "Set-Cookie". You could parse that header and extract the value with the key "NPWEBCONSOLE_XSRF-TOKEN".

Best Regards,

Ruggero

---------------------------------------------

When applicable please mark the appropriate replies as CORRECT https://community.qlik.com/docs/DOC-14806. This will help community members and Qlik Employees know which discussions have already been addressed and have a possible known solution. Please mark threads as HELPFUL if the provided solution is helpful to the problem, but does not necessarily solve the indicated problem. You can mark multiple threads as HELPFUL if you feel additional info is useful to others.



Best Regards,
Ruggero
---------------------------------------------
When applicable please mark the appropriate replies as CORRECT. This will help community members and Qlik Employees know which discussions have already been addressed and have a possible known solution. Please mark threads with a LIKE if the provided solution is helpful to the problem, but does not necessarily solve the indicated problem. You can mark multiple threads with LIKEs if you feel additional info is useful to others.
marcello
Partner - Contributor III
Partner - Contributor III
Author

Well, i am realizing there is no way using JQuery to achieve the result.

It seems there is no way to read the response header, cause browser security settings.

So, again, no way to get the required cookie value to set X-XSRF-TOKEN according the documentation.

Ruggero, you have been very kind and useful to my investigation even if i did not achieve the hoped result.

So i think the definitive answer is : if you want to do that you cannot do using a jquery script in a html page.

With kind regards, Marcello