• SGupta1
  • NEWBIE
  • 0 Points
  • Member since 2013

  • Chatter
    Feed
  • 0
    Best Answers
  • 1
    Likes Received
  • 0
    Likes Given
  • 4
    Questions
  • 2
    Replies

 

 

Hi 

 

I want to upload  a word document  on Box( External Storage)  using Rest API calls  . 

 

Since i need blob as a valid UTF -8 string I am encoding first in base 64 using Encoding util class and then decoding it back as a blob and sending request using req.setbodyasblob() , 

 

I have used all the required parameters but I am getting below invalid request error .

 

{"type":"error","status":400,"code":"invalid_request_parameters","help_url":"http:\/\/developers.box.com\/docs\/#errors","message":"Invalid input parameters in request","request_id":"10130606285252db06ef703"}

 

can someone help ?

 

 

 

  public HTTPResponse uploadFile3()
{

   Document file =[Select Body, BodyLength, Id, Name, Type, ContentType From Document ];
      
      
        
       
        String p_fileName =file.Name+'.'+file.Type;
        String parent_id = '1121945745';
        String boundary = 'AaBbCcX30';
       
      
        
        
        String header = 'Content:\r\n--' + Boundary + '\r\n'+'Content-Disposition: form-data; name="files";filename="'+p_fileName+ '"\r\n'+ 'Content-Type:'+file.ContentType+'\r\n\r\n';
        header += '--' + Boundary +'\r\n'+'Content-Disposition: form-data; name="parent_id"'+'\r\n\r\n'+'0'+'\r\n' ;
         
        
       
        system.debug('header' +header);
        String footer = '\n--'+boundary+'--';
        system.debug('footer' +footer);
  // no trailing padding on header by adding ' ' before the last "\n\n" characters
  String headerEncoded = EncodingUtil.base64Encode(Blob.valueOf(header+'\n\n'));
  system.debug('headerEncoded' +headerEncoded);
  //this ensures no trailing "=" padding
  while(headerEncoded.endsWith('='))
  {
   header+=' ';
   headerEncoded = EncodingUtil.base64Encode(Blob.valueOf(header+'\n\n'));
   system.debug('headerEncoded@1' +headerEncoded);
  }
  //base64 encoded body
  String bodyEncoded = EncodingUtil.base64Encode(file.body);
  system.debug('bodyEncoded' +bodyEncoded);
  //base64 encoded footer
  String footerEncoded = EncodingUtil.base64Encode(Blob.valueOf(footer));
    system.debug('footerEncoded' +footerEncoded);
  Blob bodyBlob = null;
  //last encoded body bytes
  String last4Bytes = bodyEncoded.substring(bodyEncoded.length()-4,bodyEncoded.length());
  system.debug('last4Bytes' +last4Bytes);
  //if the last 4 bytes encoded base64 ends with the padding character (= or ==) then re-encode those bytes with the footer
  //to ensure the padding is added only at the end of the body
  if(last4Bytes.endsWith('='))
  {
   system.debug('inside last4 bytes');
   Blob  decoded4Bytes = EncodingUtil.base64Decode(last4Bytes);
   HttpRequest tmp = new HttpRequest();
   tmp.setBodyAsBlob(decoded4Bytes);
   String last4BytesFooter = tmp.getBody()+footer;   
   system.debug('inside last4 bytes@1'+last4BytesFooter);
   system.debug('inside last4 bytes@3'+EncodingUtil.base64Encode(Blob.valueOf(last4BytesFooter)));
   system.debug('inside last4 bytes@2'+ bodyEncoded.substring(0,bodyEncoded.length()-4));
  
   bodyBlob = EncodingUtil.base64Decode(headerEncoded+bodyEncoded.substring(0,bodyEncoded.length()-4)+EncodingUtil.base64Encode(Blob.valueOf(last4BytesFooter)));
   system.debug('bodyBlob' +bodyBlob);
  }
  else
  {
   bodyBlob = EncodingUtil.base64Decode(headerEncoded+bodyEncoded+footerEncoded);
  }
   
//  if(bodyBlob.size()>3000000)
//  { 
   //this a "public class CustomException extends Exception{}"
 //  throw new CustomException('File size limit is 3 MBytes');
 // }
   
    Http h = new Http();
        HttpRequest req = new HttpRequest();
        string sUrl = 'https://upload.box.com/api/2.0/files/content';
        req.setHeader('Authorization', 'Bearer ' + accessToken);
        req.setHeader('Content-Type','multipart/form-data;boundary='+boundary);
        req.setHeader('Content-Encoding','base64');
      
        req.setEndpoint(sUrl);
        
        req.setMethod('POST');
        req.setBodyAsBlob(bodyBlob);
        System.debug('LAST ENCODING: '+EncodingUtil.base64Encode(req.getBodyAsBlob()));
  
   HttpResponse res;
        if(!Test.isRunningTest()){
            res = h.send(req);
        }
        else{
             res = new HttpResponse();
        }
        valuetoShow = 'Get Doc: ' + res.getBody(); 
        return res;
      

 

Hi 

 

I want to upload  a word document  on Box( External Storage)  using Rest API calls  . 

 

I am encoding the body in base 64 using Encoding util class but on box the content of uploaded file is in encrypted form .

 

Is there any other way to send the word /ppt  docs so that the content dont get encrypted on Box( External Storage) ?

 

 

 private HttpResponse UploadFile(){
        Document Doc =[Select Body, BodyLength, Id, Name, Type, ContentType From Document ];
        System.debug('Doc' + Doc);
        
  
        
        String p_fileName =doc.Name+'.'+doc.Type;
        String parent_id = '1121945745';
        String boundary = 'AaBbCcX30';
        

        
       String content = 'Content:\r\n--' + Boundary + '\r\n'+'Content-Disposition: form-data; name="files";filename="'+p_fileName+ '"\r\n'+ 'Content-Type:'+doc.ContentType+'\r\n\r\n';
content += '\r\n'+EncodingUtil.base64Encode(Doc.Body)+'\r\n';
content +='--' + Boundary +'\r\n'+'Content-Disposition: form-data; name="parent_id"'+'\r\n\r\n'+'0'+'\r\n' ;
content+= '--' + Boundary + '--\r\n' ;

       

        Http h = new Http();
        HttpRequest req = new HttpRequest();
        string sUrl = 'https://upload.box.com/api/2.0/files/content';
        req.setHeader('Authorization', 'Bearer ' + accessToken);
        req.setHeader('Content-Type','multipart/form-data;boundary='+boundary);
        req.setHeader('Content-Encoding','base64');
       
        req.setEndpoint(sUrl);
        
        req.setMethod('POST');
        req.setBody(content);
        
        HttpResponse res;
        if(!Test.isRunningTest()){
            res = h.send(req);
        }
        else{
             res = new HttpResponse();
        }
        valuetoShow = 'Get Doc: ' + res.getBody(); 
        return res;
      
        
      
    }

 

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 '';
          }
          
       
    }
    

 

 

Hi Anybody worked on Box Integration with Rest API ?
  • September 09, 2013
  • Like
  • 0

 

 

Hi 

 

I want to upload  a word document  on Box( External Storage)  using Rest API calls  . 

 

Since i need blob as a valid UTF -8 string I am encoding first in base 64 using Encoding util class and then decoding it back as a blob and sending request using req.setbodyasblob() , 

 

I have used all the required parameters but I am getting below invalid request error .

 

{"type":"error","status":400,"code":"invalid_request_parameters","help_url":"http:\/\/developers.box.com\/docs\/#errors","message":"Invalid input parameters in request","request_id":"10130606285252db06ef703"}

 

can someone help ?

 

 

 

  public HTTPResponse uploadFile3()
{

   Document file =[Select Body, BodyLength, Id, Name, Type, ContentType From Document ];
      
      
        
       
        String p_fileName =file.Name+'.'+file.Type;
        String parent_id = '1121945745';
        String boundary = 'AaBbCcX30';
       
      
        
        
        String header = 'Content:\r\n--' + Boundary + '\r\n'+'Content-Disposition: form-data; name="files";filename="'+p_fileName+ '"\r\n'+ 'Content-Type:'+file.ContentType+'\r\n\r\n';
        header += '--' + Boundary +'\r\n'+'Content-Disposition: form-data; name="parent_id"'+'\r\n\r\n'+'0'+'\r\n' ;
         
        
       
        system.debug('header' +header);
        String footer = '\n--'+boundary+'--';
        system.debug('footer' +footer);
  // no trailing padding on header by adding ' ' before the last "\n\n" characters
  String headerEncoded = EncodingUtil.base64Encode(Blob.valueOf(header+'\n\n'));
  system.debug('headerEncoded' +headerEncoded);
  //this ensures no trailing "=" padding
  while(headerEncoded.endsWith('='))
  {
   header+=' ';
   headerEncoded = EncodingUtil.base64Encode(Blob.valueOf(header+'\n\n'));
   system.debug('headerEncoded@1' +headerEncoded);
  }
  //base64 encoded body
  String bodyEncoded = EncodingUtil.base64Encode(file.body);
  system.debug('bodyEncoded' +bodyEncoded);
  //base64 encoded footer
  String footerEncoded = EncodingUtil.base64Encode(Blob.valueOf(footer));
    system.debug('footerEncoded' +footerEncoded);
  Blob bodyBlob = null;
  //last encoded body bytes
  String last4Bytes = bodyEncoded.substring(bodyEncoded.length()-4,bodyEncoded.length());
  system.debug('last4Bytes' +last4Bytes);
  //if the last 4 bytes encoded base64 ends with the padding character (= or ==) then re-encode those bytes with the footer
  //to ensure the padding is added only at the end of the body
  if(last4Bytes.endsWith('='))
  {
   system.debug('inside last4 bytes');
   Blob  decoded4Bytes = EncodingUtil.base64Decode(last4Bytes);
   HttpRequest tmp = new HttpRequest();
   tmp.setBodyAsBlob(decoded4Bytes);
   String last4BytesFooter = tmp.getBody()+footer;   
   system.debug('inside last4 bytes@1'+last4BytesFooter);
   system.debug('inside last4 bytes@3'+EncodingUtil.base64Encode(Blob.valueOf(last4BytesFooter)));
   system.debug('inside last4 bytes@2'+ bodyEncoded.substring(0,bodyEncoded.length()-4));
  
   bodyBlob = EncodingUtil.base64Decode(headerEncoded+bodyEncoded.substring(0,bodyEncoded.length()-4)+EncodingUtil.base64Encode(Blob.valueOf(last4BytesFooter)));
   system.debug('bodyBlob' +bodyBlob);
  }
  else
  {
   bodyBlob = EncodingUtil.base64Decode(headerEncoded+bodyEncoded+footerEncoded);
  }
   
//  if(bodyBlob.size()>3000000)
//  { 
   //this a "public class CustomException extends Exception{}"
 //  throw new CustomException('File size limit is 3 MBytes');
 // }
   
    Http h = new Http();
        HttpRequest req = new HttpRequest();
        string sUrl = 'https://upload.box.com/api/2.0/files/content';
        req.setHeader('Authorization', 'Bearer ' + accessToken);
        req.setHeader('Content-Type','multipart/form-data;boundary='+boundary);
        req.setHeader('Content-Encoding','base64');
      
        req.setEndpoint(sUrl);
        
        req.setMethod('POST');
        req.setBodyAsBlob(bodyBlob);
        System.debug('LAST ENCODING: '+EncodingUtil.base64Encode(req.getBodyAsBlob()));
  
   HttpResponse res;
        if(!Test.isRunningTest()){
            res = h.send(req);
        }
        else{
             res = new HttpResponse();
        }
        valuetoShow = 'Get Doc: ' + res.getBody(); 
        return res;
      

 

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 '';
          }
          
       
    }
    

 

 

Hi Anybody worked on Box Integration with Rest API ?
  • September 09, 2013
  • Like
  • 0