function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
SGupta1SGupta1 

Downloading a document using Rest API

Hi 

 

I want to download a word document stored on Box( External Storage) into my system using Rest API calls 

 

before downloading i need to decode the document as its uploaded using base 64 Encoding Util class 

 

but when i decode it back it gives me Blob[0]  , Perhaps its not getting decoded 

Can someone help me on this ?

 

I am using below code :

 

 private String downloadFile(){
        system.debug('Call DownloadFile');
        Blob body;
        String sFilename='';
    
        Http h = new Http();
        HttpRequest req = new HttpRequest();
    
       string endPointValue = 'https://api.box.com/2.0/files/10777849377/content';
     
        req.setEndpoint(endPointValue);
        req.setHeader('Authorization', 'Bearer ' + accessToken);
       
        req.setMethod('GET');
        System.debug('req  '+req);
        HttpResponse res = h.send(req);
        system.debug('res '+res);
        // if(!Test.isRunningTest()){
         system.debug('Body '+res.getBody());
    

         system.debug('-get headers---'+res.getHeader('Location'));
        
          body= Encodingutil.base64Decode(res.getBody());
          system.debug('Body Decoded '+body);
       
       
         if(res.getHeader('Location')!=null)
         
          {
             return res.getHeader('Location')+'';
          } else {
          return '';
          }
          
       
    }
    

 

 

SGupta1SGupta1

hi Vinita

 

I guess you are telling about Upload .

 

I have already uploaded the document using  base64Encode(Blob) 

 

Now i want to decode and download the same document  

 

I am trying to decode like this :    

 

  HttpResponse res = h.send(req);
        system.debug('res '+res);
        // if(!Test.isRunningTest()){
         system.debug('Body '+res.getBody());
    

         system.debug('-get headers---'+res.getHeader('Location'));
        
          body= Encodingutil.base64Decode(res.getBody());
          system.debug('Body Decoded '+body);

 

but the response body is blank , not sure why ? 

can anyone help ?

Vinita_SFDCVinita_SFDC

Hello,

So for header are getting reponse, is that not blank?  Try converting blob to string:

 String bodyString = body.toString();