1

I am trying to upload a file in Citrix ShareFile via REST API. There are four requests

  1. Get the access_token
  2. Get the folder URI's to decide n which I am going to upload the file.
  3. Get chunk Uri
  4. Upload the file in that chunk Uri

The first three steps work perfectly. When I am trying to POST a request for the no 4. part, it is creating a file in sharefile but with size of 0.

I have tried that with POSTMAN, and with form-data and the selection of a file, it is working fine. The file is uploaded in sharefile.

The problem arises when I am trying to upload blob data. I am calling this methods from Salesforce Platform and in Salesforce, I cannot get the physical file in a physical location. I have blob file data stored in salesforce database.

The below part works well.

String fileName_m = 'a12.pdf';  

//  Get Authentication Token

HttpRequest req = new HttpRequest();    

req.setEndpoint('https://{server}.sf-api.com/oauth/token?grant_type=password&client_id={client_id}&client_secret={secret}&username={user_name}&password={password}!');        
req.setMethod('GET');            

Http http = new Http();        
HttpResponse response = http.send(req);                   
System.Debug(response.getBody());


Map<String, Object> results = (Map<String, Object>)JSON.deserializeUntyped(response.getBody());
String token = '';
String refreshtoken = '';
if(response.getStatusCode() == 200){
 token = (String)results.get('access_token');
 refreshtoken = (String)results.get('refresh_token');
}

system.debug('token=' + token);




// Get Folder ID
HttpRequest req1 = new HttpRequest();    

req1.setEndpoint('https://{server}.sf-api.com/sf/v3/Items?$expand=Children');        
req1.setMethod('GET');   
req1.setHeader('Authorization','Bearer ' + token);       

Http http1 = new Http();        
HttpResponse response1 = http1.send(req1);                   
System.Debug(response1.getBody());


Map<String, Object> results1 = (Map<String, Object>)JSON.deserializeUntyped(response1.getBody());
String url = '';
if(response1.getStatusCode() == 200){
 url = (String)results1.get('url');
}

System.Debug('Folder Url ' + url);




// Get Chunk URI    
HttpRequest req2 = new HttpRequest();    

String endPoint = url + '/Upload2'; 
req2.setEndpoint(endPoint);        
req2.setMethod('POST');   
req2.setHeader('Authorization','Bearer ' + token);    
req2.setHeader('Content-Type', 'application/x-www-form-urlencoded');

String payload1 = 'FileName='+fileName_m;    
req2.setBody(payload1);

Http http2 = new Http();        
HttpResponse response2 = http2.send(req2);                   


Map<String, Object> results2 = (Map<String, Object>)JSON.deserializeUntyped(response2.getBody());
String ChunkUri = '';
if(response2.getStatusCode() == 200){
 ChunkUri = (String)results2.get('ChunkUri');
}

System.Debug('ChunkUri' + ChunkUri);

But, when I am trying to upload blob file data in sharefile, the API responds with a 200 status code. In sharefile, the file is created, but the size of the file is 0.

// Problem is here, it is not working
// Upload File
Attachment att = [SELECT Id, Body, Name, ContentType FROM Attachment WHERE Id='00P3p00001XNLOHEA5'];
String body = EncodingUtil.base64Encode(att.body); 



List<Attachment> cvList = [SELECT Id, Body, Name, ContentType FROM Attachment WHERE Id='00P3p00001XNLOHEA5'];

String attachmentBody = EncodingUtil.base64Encode(cvList[0].Body);

Blob pdfBlob = EncodingUtil.base64Decode(attachmentBody);

system.debug('pdfBlob' + pdfBlob);


HttpRequest req3 = new HttpRequest();    
    
    req3.setEndpoint(ChunkUri);        
    req3.setMethod('POST');   
    req3.setHeader('Authorization','Bearer ' + token);   

    req3.setHeader('Content-Length', String.valueOf(attachmentBody.length()));
    req3.setHeader('Content-Encoding', 'UTF-8');
    req3.setHeader('Content-type', 'application/pdf');
    req3.setHeader('Connection', 'keep-alive');
    req3.setBodyAsBlob(pdfBlob);


    Http http3 = new Http();        
    HttpResponse response3 = http3.send(req3);                   
    System.Debug(response3.getBody());

How to solve the issue?

Noor A Shuvo
  • 2,639
  • 3
  • 23
  • 48

0 Answers0