Use Jasperserver REST API with nodejs

205 views Asked by At

I have a nodejs app and I want to get a report (PDF format) from my Jaspersoft server using the REST API

this is my code:

axios.get("http://localhost:8080/jasperserver/rest_v2/resources/reports/Report/test", {
            params: {
              j_username: "jasperadmin", j_password: "jasperadmin"
            }
          })
          .then(function (response) {
            // handle success
            console.log("getReport response: ",response.data);
            resolve(response.data);
          })
          .catch(function (error) {
            // handle error
            console.log("getReport erreur ",error.message);
            reject({
                status: 201,
                timestamp: new Date().getTime(),
                message: "Erreur lors de l'execution",
                success: false,
              });
          })
          .finally(function () {
            // always executed
          });

I got a succeful response but the data looks like an object with only metadata, I cannot get the PDF file

1

There are 1 answers

0
GisCat On BEST ANSWER

I found the solution while reading the JasperReports Server Web Service Guide

axios.get("http://localhost:8080/jasperserver/rest_v2/reports/reports/GDO_Report/test.pdf", {
            params: {
               j_username: "jasperadmin", j_password: "jasperadmin",  
            },
            responseType: "stream"
          })
          .then(function (response) {
            // handle success
            console.log("getReport response: ",response.headers);

            //if you want to use/save the file in the back-end
            // const pdfFilePath = 'Test.pdf';
            // const pdfStream = fs.createWriteStream(pdfFilePath);
            // response.data.pipe(fs.createWriteStream(pdfFilePath))

            resolve(response.data);
          })
          .catch(function (error) {
            // handle error
            console.log("getReport erreur ",error.message);
            reject({
                status: 201,
                timestamp: new Date().getTime(),
                message: "Erreur lors de l'execution",
                success: false,
              });
          })
          .finally(function () {
            // always executed
            console.log("getReport finaly ");
          });

Hope it helps