Uploading Reports With Subreports Using REST API
Requirement:
To upload reports having subreports using REST API.
Solution:
A report unit contains mostly references to the files that make up a report within the server, like input controls, subreports, etc. So, a report unit is a composite resource that may contain other local resources. In this case, the URIs that it references include a URI in the following format:
<reportUnitURI>_files/<localResourceID>
So for example, say, we have a main .jrxml report - testSubreport.jrxml and this main .jrxml report has its one subreport - test.jasper and both these reports are created locally in Studio, and we do not have the subreport already uploaded to the JasperReports Server yet and we will publish both the main .jrxml report and its subreport using REST API. In this case, before we perform the upload with REST API, we need to provide the subreportExpression inside the source of our main .jrxml report in the above format i.e <reportUnitURI>_files/<localResourceID>
So from the above example, this format would look like below, considering we are uploading the report to our JasperReports Server repository under /public/Samples/Reports path:
/public/Samples/Reports/testSubreport_files/test
And hence, the subreportExpression inside the source of our main .jrxml report would be like:
<subreport>
…
<subreportExpression><![CDATA["/public/Samples/Reports/testSubreport_files/test"]]></subreportExpression>
</subreport>
With the above information, we are now ready to proceed with the upload of our main and subreport using REST API.
We will now use a POST rest_v2/resources service and send a complete body descriptor containing our main and subreport in base64EncodedContent.
POST http://<host>:<port>/jasperserver[-pro]/rest_v2/resources/path/to/folder
Content-Type: application/repository.<resourceType>+json
In our example, the request and the content type would look like:
POST http://localhost:8080/jasperserver-pro/rest_v2/resources/public/Samples/Reports
Content-Type: application/repository.reportUnit+json
For more information on creating a resource using REST API, see “Creating a Resource” section from our REST API guide:
https://community.jaspersoft.com/documentation/tibco-jasperreports-server-rest-api-reference/v790/resources-service#:~:text=in%20the%20repository.-,Creating%20a%20Resource,-The%20POST%20and
And general structure of the body descriptor should be like below:
{
"uri": "{resourceUri}",
"label": "Sample Label",
"permissionMask": "0",
"version": 0,
"dataSource": {
"dataSourceReference": {
"uri": "{dataSourceUri}"
}
},
"jrxml": {
"jrxmlFile": {
"label": "Main jrxml",
"type": "jrxml",
"content": "{base64encodedContent}"
}
},
"inputControls": [
{
"inputControlReference": {
"uri": "{inputControlUri}"
}
},
...
],
"resources": {
"resource": [
{
"name": "{resourceName}",
"file" : {
"fileResource": {
"label": "{label}",
"type": "jrxml",
"content": "{base64EncodedContent}"
}
}
},
{
"name": "{resourceName}",
"file": {
"fileReference": {
"uri": "{uri}"
}
}
},
...
]
}
}
The above descriptor is for a report unit. For more information on other resources’ descriptors, see “Resource Descriptors” section from our REST API guide:
https://community.jaspersoft.com/documentation/tibco-jasperreports-server-rest-api-reference/v790/resource-descriptors
Thus, following the above we can publish a report with its subreport(s) using REST API.
A test.json file can be found in the attachments section of this article, which can be run using a POST request in a REST-tool, like Postman. The report, once uploaded, can be run using the sample Foodmart DB on JasperReports Server UI.
POST http://localhost:8080/jasperserver-pro/rest_v2/resources/public/Samples/Reports
Header:
Content-Type: application/repository.reportUnit+json
-
Uploading Reports With Subreports Using REST API
Table of contents
User Feedback
Recommended Comments
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now