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

左心房为你撑大大i 提交于 2019-11-30 05:05:46

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!

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
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!