staggart
staggart

Reputation: 368

Need Example of passing Jasper Reports Parameters for REST v2 API using JSON

When I look at the documentation for passing parameters to the Jasper Report REST 2 API here: http://community.jaspersoft.com/documentation/jasperreports-server-web-services-guide/v550/running-report-asynchronously I see that I need to have a "parameters" dict. The example in the link shows the XML which is not all that useful since it's unclear exactly what the equivalent JSON should look like. The closest I could find is in this link: http://community.jaspersoft.com/documentation/jasperreports-server-web-services-guide/v56/modifying-report-parameters. Now, I am sending the equivalent of that to the server (and every other permutation I can think of), and I continue to get a "400 Client Error: Bad Request" back. I could really use an exact example of the python code to generate the required "parameters" parameter for say "my_parameter_1="test_value_1".

Here is my current POST data (with a few params missing for brevity). I know this is correct since the report works fine if I omit the "parameters" parameter:

    {
      'outputFormat': 'pdf', 
      'parameters': [{'name': 'ReportID', 'value': ['my_value_1']}], 
      'async': 'true', 
      'pages': '', 
      'interactive': 'false'
    }

Upvotes: 4

Views: 11605

Answers (4)

Eric
Eric

Reputation: 254

Here is an example that worked for me. Im using Python 2.7, and the community edition of Jaspersoft. Like the C# example above, this example also uses the rest v2 which made it very simple for me to download a pdf report quickly

import requests

sess = requests.Session()   
auth = ('username', 'password')
res = sess.get(url='http://your.jasper.domain:8080/jasperserver/', auth=auth)
res.raise_for_status()
url = 'http://your.jasper.domain:8080/jasperserver/rest_v2/reports/report_folder/sub_folder/report_name.pdf'
params = {'Month':'2', 'Year':'2017','Project': 'ProjectName'}
res = sess.get(url=url, params=params, stream=True)
res.raise_for_status()
path = '/path/to/Downloads/report_name.pdf'
with open(path, "wb") as f:
    f.write(res.content)

Upvotes: 1

Lester
Lester

Reputation: 750

Here's a full example about generate a report using Rest V2, in my case it's running on C#:

try {
    var server = "http://localhost:8080/jasperserver";
    var login = server + "/rest/login";
    var report = "/rest_v2/reports/organization/Reports/report_name.pdf";
    var client = new WebClient();

    //Set the content type of the request
    client.Headers[HttpRequestHeader.ContentType] = "application/x-www-form-urlencoded";

    //Set the username and password
    NameValueCollection parametros = new NameValueCollection();
    parametros.Add("j_username", "jasperadmin");
    parametros.Add("j_password", "123456");

    //Request to login
    client.UploadValues(login, "POST", parametros);

    //Get session cookie
    string session = client.ResponseHeaders.Get("Set-Cookie");

    //Set session cookie to the next request
    client.Headers.Add("Cookie", session);

    //Generate report with parameters: "start" and "end"
    var reporte = client.DownloadData(server + report + "?start=2015-10-01&end=2015-10-10");

    //Returns the report as response
    return File(reporte, "application/pdf", "test.pdf");
}catch(WebException e){
    //return Content("There was a problem, status code: " + ((HttpWebResponse)e.Response).StatusCode);
    return null;
}

Upvotes: 0

BoomZilla
BoomZilla

Reputation: 746

Nice Job there Staggart. I got it now. Because I wasn't reading with max. scrutinity, I wasted some additional time. So the interested coder is not only advised to be aware of the nested, syntactictally interesting reportParameter-property, but especially that the value-property inside that is an array. I suppose one could pass some form of Lists/Arrays/Collections here?

What irritated me was, if I should construct more than one "reportParameter" property, but that would be nonsense according to Does JSON syntax allow duplicate keys in an object.

So just for the record, how to post multiple parameters:

{
    "reportUnitUri": "/reports/Top10/Top10Customers",
    "async": true,
    "freshData": true,
    "saveDataSnapshot": false,
    "outputFormat": "pdf",
    "interactive": false,
    "ignorePagination": true,
    "parameters": {
        "reportParameter": [
            {
                "name": "DATE_START_STRING",
                "value": ["14.07.2014"]
            },
            {
                "name": "DATE_END_STRING",
                "value": ["14.10.2014"]
            }
        ]
    }
}

If someone accidently is struggling with communicating with jasper via REST and PHP. Do yourself a favour and use the Requests for PHP instead of pure CURL. It even has a fallback for internally using Sockets instead of CURL, when latter isn't available.

Upvote for you Staggart.

Upvotes: 6

staggart
staggart

Reputation: 368

OK, thanks to rafkacz1 @ http://community.jaspersoft.com/questions/825719/json-equivalent-xml-post-reportexecutions-rest-service who posted an answer, I figured it out. As he report there, the required format is:

    "parameters":{
         "reportParameter":[
             {"name":"my_parameter_1","value":["my_value_1"]}
          ]
     }

Pay particular attention to the plurality of "reportParameter".

Upvotes: 5

Related Questions