Send headers on ALL Visualize calls

Our JavaScript code uses Visualize.js to run, render and/or export reports. Internally Visualize.js sends requests to JasperReports to run these reports, check status, download CSS, JS and IMG files among other things. We would like to send a custom request header on each of these calls. Visualize.js documentation seem to suggest there is an option to send  http "headers" on the 'auth' properties. Is this possible, are there some examples?

thanks

 

fc-reporting's picture
Joined: Feb 21 2018 - 6:33am
Last seen: 4 years 6 months ago

Hello there,

 

Even I'm looking for similar solution to add HTTP header for request sent via visualise.js library API to jaspersoft server. The solution mentioned in th answer section, only adds HTTP header for login request. I want to add it for custom validation purpose. So, all request sent to jasper server are validated via "auth_token' HTTP header by our custom validator. Requests with appropriate access right, reach jasper server and all other requests are rejected by our custom validator.

kenildomadia - 4 years 4 months ago

2 Answers:

What sort of header do you want to add and why?

You can add headers into the loginFn of the authentication. Visualize does this itself for username/password authentication. Note the headers: in the request.


 

    var JrsAuthenticationExecutor = {
        login: function(properties, request){
            var dfd = $.Deferred();
            request({
                url: properties.url + (properties.preAuth ? "" : "/j_spring_security_check") + "?" + getParametersString(properties),
                headers: {
                    "Accept":"application/json"
                }
            }).done(function (response, a, b) {
                var result = response;
                if(typeof response === "string"){
                    try {
                        result = JSON.parse(response);
                    }catch (error){
                        dfd.reject(error);
                    }
                }
                if(result.success === true){
                    dfd.resolve(result);
                } else {
                    dfd.reject(b);
                }
            }).fail(function (xhr) {
                dfd.reject(xhr);
            });
            return dfd;
        },
        logout: function(properties, request){
            return request({
                url: properties.url + "/logout.html"
            });
        }
    };

This is only for the initial authentication request to the JasperReports Server, which establishes the user session on JRS.

Do you need to add the headers for every request?

Sherman

swood_1's picture
9088
Joined: Nov 15 2012 - 10:47am
Last seen: 2 years 11 months ago

In order to set the headers on every visualize request, you can hook into the underlying xhr machinery like this:

visualize(function(visualizeClient) {
    __visualize__.require(["jquery"], function(jq) {
        jq.ajaxSetup({
            beforeSend: function(xhr) {
                xhr.setRequestHeader("A-Header", "headerValue");
            }
        });
 
        visualizeClient.report({
            server: "<?= $restUrl ?>",
....

swood_1's picture
9088
Joined: Nov 15 2012 - 10:47am
Last seen: 2 years 11 months ago
Feedback
randomness