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
nindranindra 

Is using REST API only way to upload documents to BOX in salesforce??

Is using REST API only way to upload documents to BOX in salesforce?? Can we upload document without using REST APIs
NagendraNagendra (Salesforce Developers) 
Hi Nindra,

Please check with below sample code for a similar requirement.
public String uploadFileToBox(String strFolderId,Document file,String token)
     {
        String boundary = '----------------------------741e90d31eff';
        String header = '--'+boundary+'\nContent-Disposition: form-data; name="file"; filename="'+file.name+'";\nContent-Type: multipart/form-data;'+'\nnon-svg='+True;
        String footer = '--'+boundary+'--';             
        String headerEncoded = EncodingUtil.base64Encode(Blob.valueOf(header+'\r\n\r\n'));
        HttpResponse res;
        String strFileId;
      
        while(headerEncoded.endsWith('='))
        {
            header+=' ';
            headerEncoded = EncodingUtil.base64Encode(Blob.valueOf(header+'\r\n\r\n'));
        }
      
        String bodyEncoded = EncodingUtil.base64Encode(file.body);
    
        Blob bodyBlob = null;
        String last4Bytes = bodyEncoded.substring(bodyEncoded.length()-4,bodyEncoded.length());
        
        // GW: replacement section to get rid of padding without corrupting data
        if(last4Bytes.endsWith('==')) 
        {
              last4Bytes = last4Bytes.substring(0,2) + '0K';
            bodyEncoded = bodyEncoded.substring(0,bodyEncoded.length()-4) + last4Bytes;
            String footerEncoded = EncodingUtil.base64Encode(Blob.valueOf(footer));
            bodyBlob = EncodingUtil.base64Decode(headerEncoded+bodyEncoded+footerEncoded);
        }
         
        else if(last4Bytes.endsWith('=')) 
        {
              last4Bytes = last4Bytes.substring(0,3) + 'N';
            bodyEncoded = bodyEncoded.substring(0,bodyEncoded.length()-4) + last4Bytes;
            footer = '\n' + footer;
            String footerEncoded = EncodingUtil.base64Encode(Blob.valueOf(footer));
            bodyBlob = EncodingUtil.base64Decode(headerEncoded+bodyEncoded+footerEncoded);              
        } 
        
        else 
        {
            footer = '\r\n' + footer;
            String footerEncoded = EncodingUtil.base64Encode(Blob.valueOf(footer));
            bodyBlob = EncodingUtil.base64Decode(headerEncoded+bodyEncoded+footerEncoded);  
        }
    
        String sUrl = 'https://upload.box.com/api/2.0/files/content?parent_id='+strFolderId;
          HttpRequest req = new HttpRequest();
      
          req.setHeader('Content-Type','multipart/form-data;non_svg='+True+';boundary='+boundary);

          req.setMethod('POST');
          req.setEndpoint(sUrl);
         req.setBodyAsBlob(bodyBlob);
          req.setTimeout(60000);
          req.setHeader('Authorization', 'Bearer '+token);
          req.setHeader('Content-Length',String.valueof(req.getBodyAsBlob().size()));
      
          Http http = new Http();
          
          system.debug('*****size****'+req.getBodyAsBlob().size());
          
        if(Test.isRunningTest()) {
                
                // Create a fake response
            res = new HttpResponse();
            res.setStatusCode(201);
        }
        else {
            
               res = http.send(req);
        }
      
        
    }
Regards,
Nagendra.