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'
    }

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.


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 ".


下面是使用Rest V2生成报告的完整示例,在我的案例中它运行在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;
}
链接地址: http://www.djcxy.com/p/37834.html

上一篇: 将具有重复键的JSON对象转换为JSON数组

下一篇: 需要使用JSON为REST v2 API传递Jasper报表参数的示例