I need help on how to download the pbix report file from PowerBI using restAPI call in java springboot. Please find the code below that I am trying to achieve the same.
****restcontroller***
@PostMapping(value = "/powerbi/download/{id}")
public ResponseEntity<byte[]> downloadPowerBIReport(@PathVariable("id") String reportId, @RequestParam String reportName) {
try {
//groupId and accessToken taken from global variables
return powerBIService.exportPowerBIReport( reportId, groupId, reportName, accessToken);
} catch (Exception ex) {
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
.body(ex.getMessage().getBytes());
}
}
****service***
public ResponseEntity<byte[]> exportPowerBIReport(String reportId, String groupId, String reportName,String accessToken) throws Exception {
String exportUrl = String.format("%s/%s/reports/%s/Export", powerbiGroupUrl, groupId, reportId);
String body = "{\"format\": \"PBIX\"}";
HttpEntity<String> entity = new HttpEntity<>(body,createHeaders());
ResponseEntity<JsonNode> response = restTemplate.exchange(exportUrl, HttpMethod.POST, entity, JsonNode.class); //execution breaks here
if (response.getStatusCode() == HttpStatus.ACCEPTED) {
// Step 2: Poll for export status
String exportId = response.getBody().get("id").asText();
String statusUrl = String.format("%s/%s/reports/%s/exports/%s", powerbiGroupUrl, groupId, reportId, exportId);
// Poll until the export is complete
while (true) {
ResponseEntity<JsonNode> statusResponse = restTemplate.exchange(statusUrl, HttpMethod.GET, entity, JsonNode.class);
String status = statusResponse.getBody().get("status").asText();
if ("Succeeded".equalsIgnoreCase(status)) {
// Step 3: Download the file
String fileUrl = statusResponse.getBody().get("resourceLocation").asText();
return downloadFile(fileUrl, accessToken);
} else if ("Failed".equalsIgnoreCase(status)) {
throw new RuntimeException("Export failed. Please try again.");
}
// Wait before polling again
TimeUnit.SECONDS.sleep(5);
}
} else {
throw new RuntimeException("Failed to initiate export. Status: " + response.getStatusCode());
}
}
private ResponseEntity<byte[]> downloadFile(String fileUrl, String reportName, String accessToken) {
HttpHeaders headers = new HttpHeaders();
headers.set("Authorization", "Bearer " + accessToken);
HttpEntity<String> entity = new HttpEntity<>(headers);
ResponseEntity<byte[]> fileResponse = restTemplate.exchange(fileUrl, HttpMethod.GET, entity, byte[].class);
if (fileResponse.getStatusCode() == HttpStatus.OK) {
HttpHeaders downloadHeaders = new HttpHeaders();
downloadHeaders.set("Content-Disposition", "attachment; filename="+reportName+".pbix");
downloadHeaders.set("Content-Type", "application/octet-stream");
return new ResponseEntity<>(fileResponse.getBody(), downloadHeaders, HttpStatus.OK);
} else {
throw new RuntimeException("Failed to download PBIX file. Status: " + fileResponse.getStatusCode());
}
}
The error that I am getting
Referred this @https://learn.microsoft.com/en-us/rest/api/power-bi/reports/export-report-in-group?source=docs and tried different ways to resolve this but no success yet. One thing, I am not understanding here is that why
Please help me in resolving this. Also, please note that api endpoint '/powerbi/download/{id}', it should automatically download the report in pbix format. I want explicitly PBIX file to be exported, no other format.
Thanks in advance for help.