Get report from jasperserver using REST webservice

2019-02-04 19:10发布

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!

2条回答
Bombasti
2楼-- · 2019-02-04 19:22

Dude thanks very much this was the single most helpful post I've found on getting reports out of JasperServer via REST, and I'm using Ruby on Rails. After upgrading to JS 5.0 i found the SOAP interface very unreliable, basically you had to hit it twice for the report to generate. I had zero success with the rest_v2 interface, it never seemed to honor any parameters passed to it.

So I tried out the older REST interface and its working fine. For any Rails guys, here's my (slightly messy) console code.

report = 'TempSpeedLimit'
params = {:tsl_id => 744}
request_body = "<resourceDescriptor name='#{report}' wsType='reportUnit' uriString='/reports/hawk/#{report}' isNew='false'><label>null</label>"
params.each{|k,v| request_body += "<parameter name='#{k}'>#{v}</parameter>"}
request_body += "</resourceDescriptor>"
body_put = ''
body_get = ''
cookie_put = ''

uri_put = URI.parse("http://<yourhost>:8080/jasperserver/rest/report/reports/hawk/#{report}")
http_put = Net::HTTP.new(uri_put.host, uri_put.port)
http_put.start do |http|
req = Net::HTTP::Put.new(uri_put.path + "?RUN_OUTPUT_FORMAT=PDF")
req.basic_auth('jasperadmin', 'secretpassword')
req.body = request_body
resp = http.request(req)
body_put = resp.body
cookie_put = resp['Set-Cookie']
end

xml = REXML::Document.new(body_put)
uuid = xml.elements["report/uuid"].text

uri_get = URI.parse("http://<yourhost>:8080/jasperserver/rest/report/#{uuid}")
http_get = Net::HTTP.new(uri_get.host, uri_get.port)
http_get.start do |http|
req = Net::HTTP::Get.new(uri_get.path + "?file=report")
req.basic_auth('jasperadmin', 'secretpassword')
req['cookie'] = cookie_put
resp = http.request(req)
body_get = resp.body
end

f = File.new('test.pdf', 'wb')
f.write(body_get)
f.close
查看更多
冷血范
3楼-- · 2019-02-04 19:27

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!

查看更多
登录 后发表回答