user1398291
user1398291

Reputation: 203

How to download file (InputStream) from Microsoft graph APIs?

I am following Graph APIs to download a file from Sharepoint.

I tried this endpoint: https://graph.microsoft.com/v1.0/drives/{drive_id}/root:/{folder}/{file_name}:/content

And to fetch InputStream using restTemplate:

 HttpHeaders httpHeaders = new HttpHeaders();
        httpHeaders.setContentType(MediaType.APPLICATION_JSON);
        httpHeaders.set(GraphConstant.AUTHORIZATION, TOKEN);
        HttpEntity httpEntity = new HttpEntity(httpHeaders);

String downloadEndPoint = DOWNLOAD_FILE_ENDPOINT.replace(GraphConstant.DRIVE_ID,getDriveId(id)).replace("{folder}",folder).replace(GraphConstant.FILE_NAME, URLEncoder.encode(fileName, GraphConstant.UTF_8).replace("+", "%20"));
        ResponseEntity<InputStream> responseEntity = restTemplate.exchange(downloadEndPoint,
                HttpMethod.GET,
                httpEntity,
                InputStream.class);

        if(responseEntity.getStatusCode().equals(HttpStatus.OK)){
           return responseEntity.getBody();

        }

responseEntity.getBody() returns null.

What is the return type of Graph Download file API? Any inputs here?

I am using a SpringBoot application and making calls with restTemplate (not using Microsoft SDK to make graph calls).

Upvotes: 1

Views: 1453

Answers (1)

user1398291
user1398291

Reputation: 203

Changing ResponseEntity to ResponseEntity<byte[]> solved my problem.

ResponseEntity<byte[]> responseEntity = graphRestTemplate.exchange(DOWNLOAD_FILE_ENDPOINT,
            HttpMethod.GET,
            httpEntity,
            byte[].class,
            drive_id,
            entity_id,
            fileName); 

Upvotes: 1

Related Questions