Get report from jasperserver using REST webservice and asp.net C#

torinfo picture torinfo · Mar 8, 2012 · Viewed 16.8k times · Source

You can use the jasperservers webservices (SOAP and REST is available) to get mange and run reports on from a web application. The SOAP wsdl is not compatible with asp.net c# (at least, I cannot get it to work), so I decided to use the REST webservice.

I am ALMOST there, but I can't retrieve the report itself. does anyone know what goes wrong? I am using jasperserver CE 4.5 on Linux.

// Setup WebClient 
WebClient httpclient = new WebClient();

//Basic Auth
httpclient.Credentials = new NetworkCredential("NAME", "PASSWD");
httpclient.Headers.Add("Content-Type", "application/x-www-form-urlencoded");

// Build resourceDescriptor
string requestXml;
requestXml =  "<resourceDescriptor name="budget_overzicht_per_klant" wsType="reportUnit" uriString="/Declaraties/12change/Klant/budget_overzicht_per_klant"n";
requestXml += " isNew="false">n";
requestXml += "   <label>null</label>n";
requestXml += "   <parameter name="klantid">14</parameter>n";
requestXml += "   <parameter name="start">20120101</parameter>n";
requestXml += "   <parameter name="eind">20120302'</parameter>n";
requestXml += "   <parameter name="Titel">Test 123</parameter>n";
requestXml += "</resourceDescriptor>n";

// Send PUT
string requestAllResult = httpclient.UploadString("http://website/jasperserver/rest/report/Declaraties/12change/Klant/budget_overzicht_per_klant?RUN_OUTPUT_FORMAT=PDF", "PUT", requestXml);

// requestAllResult contains:
//<report>
//  <uuid>f521fe7d-7432-4c47-962c-9fec29bdaa43</uuid>
//  <originalUri>/Declaraties/12change/Klant/budget_overzicht_per_klant</originalUri>
//  <totalPages>4</totalPages>
//  <startPage>1</startPage>
//  <endPage>4</endPage>
//  <file type="application/pdf"><![CDATA[report]]></file>
//</report>
// You have to use the uuid to GET the file 'report'
//
// Extract uuid, filename is always report
System.Xml.XmlDocument doc = new System.Xml.XmlDocument();
doc.LoadXml(requestAllResult);
XmlNode node = doc.DocumentElement.SelectSingleNode("uuid");
string uuid = node.InnerText;

//Build GET URL
string reportUrl = "http://website/jasperserver/rest/report/";
reportUrl += uuid;
reportUrl += "?file=report";

// the value of report Url is now 
// "http://website/jasperserver/rest/report/f521fe7d-7432-4c47-962c-9fec29bdaa43?file=report"

// Get report
string report;
report = httpclient.DownloadString(reportUrl);

// Exception, HTTP 404 ERROR????

The error seems to mean that the uuid is not in the current session. Anyone got this working? Thanks!

Answer

torinfo picture torinfo · Mar 9, 2012

I'll answer my own question:

The PUT returns a Cookie in the response header, that you have to use in the subsequent GET:

// Send PUT 
string requestAllResult = httpclient.UploadString("http://website/jasperserver/rest/report/Declaraties/12change/Klant/budget_overzicht_per_klant?RUN_OUTPUT_FORMAT=PDF", "PUT", requestXml);

// Get session cookie
string session = httpclient.ResponseHeaders.Get("Set-Cookie");
// Set session cookie
httpclient.Headers.Add("Cookie", session);

// Get report
report=httpclient.DownloadString("http://website/jasperserver/rest/report/f521fe7d-7432-4c47-962c-9fec29bdaa43?file=report");

And that's it!