Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Learn from the best! Meet the four finalists headed to the FINALS of the Power BI Dataviz World Championships! Register now

Reply
vijayburnwal
New Member

Java Springboot | Download powerbi pbix report file using restAPI

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 powerbiGroupUrl="https://api.powerbi.com/v1.0/myorg/groups";
        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 
404 Not Found: "{<EOL><EOL> "error":{<EOL><EOL> "code":"","message":"No HTTP resource was found that matches the request URI 'https://wabi-us-north-central-g-primary-redirect.analysis.windows.net/v1.0/myorg/groups/6065e0ed-eb7...'."<EOL><EOL> }<EOL><EOL>}"
 
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.
 
 
 
 
1 REPLY 1
lbendlin
Super User
Super User

You requested a PBIX but then are polling the status for the "Export to File"  calls.

 

Reports - Export Report In Group - REST API (Power BI Power BI REST APIs) | Microsoft Learn

Helpful resources

Announcements
Join our Fabric User Panel

Join our Fabric User Panel

Share feedback directly with Fabric product managers, participate in targeted research studies and influence the Fabric roadmap.

March Power BI Update Carousel

Power BI Community Update - March 2026

Check out the March 2026 Power BI update to learn about new features.