Skip to main content
Made the merged in answer slightly clearer.
Source Link
Daniel Ballinger
  • 103k
  • 40
  • 275
  • 601

Final solution provided by @user2662369

    @future(callout=true)
public static void fileUploadCallout(string idStr) 
{
 string Endpoint = 'https:********';
 string xAPIKey = '***********';
 string verticalId = '*******';
 string callbackURL = '**************';

 List<contentversion> cvList = new List<contentversion>();
 cvList = [select id, title, ContentDocumentId, FileExtension, versionData from contentversion where Id = :idStr limit 1];
 System.debug('Callout Log 1:' + cvList);
 if(!cvList.isEmpty())     
 {
  //form fileName with cv ID
  string fileName = cvList[0].Id;
  if(cvList[0].FileExtension!=null && cvList[0].FileExtension!='')
  {
   fileName = fileName + '.' + cvList[0].FileExtension;  
  }         
  System.debug('Callout Log 2:' + fileName);

  //callout ePOR service
  string contentType = EinsteinVision_HttpBodyPart.GetContentType();

  //  Compose the form
  string form64 = '';
  //******This was the boundary I was missing****
  form64 += EinsteinVision_HttpBodyPart.WriteBoundary();
  form64 += EinsteinVision_HttpBodyPart.WriteBlobBodyParameter('file', EncodingUtil.base64Encode(cvList[0].versionData), fileName);
         
  blob formBlob = EncodingUtil.base64Decode(form64);
  string contentLength = string.valueOf(formBlob.size());

  System.debug('Callout Log 3:' + formBlob.size());

  if(formBlob.size()>12000000)
  {
   //this a "public class CustomException extends Exception{}"
   throw new CustomException('File size limit is 12 MBytes');
  }
  else
  {
     HttpRequest req = new HttpRequest();
     req.setMethod('POST');
     req.setEndpoint(Endpoint);
     req.setBodyAsBlob(formBlob);
     req.setHeader('apiKey', xAPIKey);
     req.setHeader('vertid', verticalId);
     req.setHeader('callback', callbackURL);
     req.setHeader('Connection', 'keep-alive');
     req.setHeader('Content-Length', contentLength);
     req.setHeader('Content-Type', contentType);
     req.setTimeout(120000);
     Http http =new Http();
     HTTPResponse res = http.send(req);
     System.debug('Callout Log 4:' + res.getStatusCode());
     System.debug('Callout Log 5:' + String.valueof(res.getbody()));

     string responseStatusCode = String.valueof(res.getStatusCode()); 
     string responseBody = String.valueof(res.getbody()); 

  }
 }
}
    @future(callout=true)
public static void fileUploadCallout(string idStr) 
{
 string Endpoint = 'https:********';
 string xAPIKey = '***********';
 string verticalId = '*******';
 string callbackURL = '**************';

 List<contentversion> cvList = new List<contentversion>();
 cvList = [select id, title, ContentDocumentId, FileExtension, versionData from contentversion where Id = :idStr limit 1];
 System.debug('Callout Log 1:' + cvList);
 if(!cvList.isEmpty())     
 {
  //form fileName with cv ID
  string fileName = cvList[0].Id;
  if(cvList[0].FileExtension!=null && cvList[0].FileExtension!='')
  {
   fileName = fileName + '.' + cvList[0].FileExtension;  
  }         
  System.debug('Callout Log 2:' + fileName);

  //callout ePOR service
  string contentType = EinsteinVision_HttpBodyPart.GetContentType();

  //  Compose the form
  string form64 = '';
  //******This was the boundary I was missing****
  form64 += EinsteinVision_HttpBodyPart.WriteBoundary();
  form64 += EinsteinVision_HttpBodyPart.WriteBlobBodyParameter('file', EncodingUtil.base64Encode(cvList[0].versionData), fileName);
         
  blob formBlob = EncodingUtil.base64Decode(form64);
  string contentLength = string.valueOf(formBlob.size());

  System.debug('Callout Log 3:' + formBlob.size());

  if(formBlob.size()>12000000)
  {
   //this a "public class CustomException extends Exception{}"
   throw new CustomException('File size limit is 12 MBytes');
  }
  else
  {
     HttpRequest req = new HttpRequest();
     req.setMethod('POST');
     req.setEndpoint(Endpoint);
     req.setBodyAsBlob(formBlob);
     req.setHeader('apiKey', xAPIKey);
     req.setHeader('vertid', verticalId);
     req.setHeader('callback', callbackURL);
     req.setHeader('Connection', 'keep-alive');
     req.setHeader('Content-Length', contentLength);
     req.setHeader('Content-Type', contentType);
     req.setTimeout(120000);
     Http http =new Http();
     HTTPResponse res = http.send(req);
     System.debug('Callout Log 4:' + res.getStatusCode());
     System.debug('Callout Log 5:' + String.valueof(res.getbody()));

     string responseStatusCode = String.valueof(res.getStatusCode()); 
     string responseBody = String.valueof(res.getbody()); 

  }
 }
}

Final solution provided by @user2662369

@future(callout=true)
public static void fileUploadCallout(string idStr) 
{
 string Endpoint = 'https:********';
 string xAPIKey = '***********';
 string verticalId = '*******';
 string callbackURL = '**************';

 List<contentversion> cvList = new List<contentversion>();
 cvList = [select id, title, ContentDocumentId, FileExtension, versionData from contentversion where Id = :idStr limit 1];
 System.debug('Callout Log 1:' + cvList);
 if(!cvList.isEmpty())     
 {
  //form fileName with cv ID
  string fileName = cvList[0].Id;
  if(cvList[0].FileExtension!=null && cvList[0].FileExtension!='')
  {
   fileName = fileName + '.' + cvList[0].FileExtension;  
  }         
  System.debug('Callout Log 2:' + fileName);

  //callout ePOR service
  string contentType = EinsteinVision_HttpBodyPart.GetContentType();

  //  Compose the form
  string form64 = '';
  //******This was the boundary I was missing****
  form64 += EinsteinVision_HttpBodyPart.WriteBoundary();
  form64 += EinsteinVision_HttpBodyPart.WriteBlobBodyParameter('file', EncodingUtil.base64Encode(cvList[0].versionData), fileName);
         
  blob formBlob = EncodingUtil.base64Decode(form64);
  string contentLength = string.valueOf(formBlob.size());

  System.debug('Callout Log 3:' + formBlob.size());

  if(formBlob.size()>12000000)
  {
   //this a "public class CustomException extends Exception{}"
   throw new CustomException('File size limit is 12 MBytes');
  }
  else
  {
     HttpRequest req = new HttpRequest();
     req.setMethod('POST');
     req.setEndpoint(Endpoint);
     req.setBodyAsBlob(formBlob);
     req.setHeader('apiKey', xAPIKey);
     req.setHeader('vertid', verticalId);
     req.setHeader('callback', callbackURL);
     req.setHeader('Connection', 'keep-alive');
     req.setHeader('Content-Length', contentLength);
     req.setHeader('Content-Type', contentType);
     req.setTimeout(120000);
     Http http =new Http();
     HTTPResponse res = http.send(req);
     System.debug('Callout Log 4:' + res.getStatusCode());
     System.debug('Callout Log 5:' + String.valueof(res.getbody()));

     string responseStatusCode = String.valueof(res.getStatusCode()); 
     string responseBody = String.valueof(res.getbody()); 

  }
 }
}
    @future(callout=true)
public static void fileUploadCallout(string idStr) 
{
 string Endpoint = 'https:********';
 string xAPIKey = '***********';
 string verticalId = '*******';
 string callbackURL = '**************';

 List<contentversion> cvList = new List<contentversion>();
 cvList = [select id, title, ContentDocumentId, FileExtension, versionData from contentversion where Id = :idStr limit 1];
 System.debug('Callout Log 1:' + cvList);
 if(!cvList.isEmpty())     
 {
  //form fileName with cv ID
  string fileName = cvList[0].Id;
  if(cvList[0].FileExtension!=null && cvList[0].FileExtension!='')
  {
   fileName = fileName + '.' + cvList[0].FileExtension;  
  }         
  System.debug('Callout Log 2:' + fileName);

  //callout ePOR service
  string contentType = EinsteinVision_HttpBodyPart.GetContentType();

  //  Compose the form
  string form64 = '';
  //******This was the boundary I was missing****
  form64 += EinsteinVision_HttpBodyPart.WriteBoundary();
  form64 += EinsteinVision_HttpBodyPart.WriteBlobBodyParameter('file', EncodingUtil.base64Encode(cvList[0].versionData), fileName);
         
  blob formBlob = EncodingUtil.base64Decode(form64);
  string contentLength = string.valueOf(formBlob.size());

  System.debug('Callout Log 3:' + formBlob.size());

  if(formBlob.size()>12000000)
  {
   //this a "public class CustomException extends Exception{}"
   throw new CustomException('File size limit is 12 MBytes');
  }
  else
  {
     HttpRequest req = new HttpRequest();
     req.setMethod('POST');
     req.setEndpoint(Endpoint);
     req.setBodyAsBlob(formBlob);
     req.setHeader('apiKey', xAPIKey);
     req.setHeader('vertid', verticalId);
     req.setHeader('callback', callbackURL);
     req.setHeader('Connection', 'keep-alive');
     req.setHeader('Content-Length', contentLength);
     req.setHeader('Content-Type', contentType);
     req.setTimeout(120000);
     Http http =new Http();
     HTTPResponse res = http.send(req);
     System.debug('Callout Log 4:' + res.getStatusCode());
     System.debug('Callout Log 5:' + String.valueof(res.getbody()));

     string responseStatusCode = String.valueof(res.getStatusCode()); 
     string responseBody = String.valueof(res.getbody()); 

  }
 }
}
    @future(callout=true)
public static void fileUploadCallout(string idStr) 
{
 string Endpoint = 'https:********';
 string xAPIKey = '***********';
 string verticalId = '*******';
 string callbackURL = '**************';

 List<contentversion> cvList = new List<contentversion>();
 cvList = [select id, title, ContentDocumentId, FileExtension, versionData from contentversion where Id = :idStr limit 1];
 System.debug('Callout Log 1:' + cvList);
 if(!cvList.isEmpty())     
 {
  //form fileName with cv ID
  string fileName = cvList[0].Id;
  if(cvList[0].FileExtension!=null && cvList[0].FileExtension!='')
  {
   fileName = fileName + '.' + cvList[0].FileExtension;  
  }         
  System.debug('Callout Log 2:' + fileName);

  //callout ePOR service
  string contentType = EinsteinVision_HttpBodyPart.GetContentType();

  //  Compose the form
  string form64 = '';
  //******This was the boundary I was missing****
  form64 += EinsteinVision_HttpBodyPart.WriteBoundary();
  form64 += EinsteinVision_HttpBodyPart.WriteBlobBodyParameter('file', EncodingUtil.base64Encode(cvList[0].versionData), fileName);
         
  blob formBlob = EncodingUtil.base64Decode(form64);
  string contentLength = string.valueOf(formBlob.size());

  System.debug('Callout Log 3:' + formBlob.size());

  if(formBlob.size()>12000000)
  {
   //this a "public class CustomException extends Exception{}"
   throw new CustomException('File size limit is 12 MBytes');
  }
  else
  {
     HttpRequest req = new HttpRequest();
     req.setMethod('POST');
     req.setEndpoint(Endpoint);
     req.setBodyAsBlob(formBlob);
     req.setHeader('apiKey', xAPIKey);
     req.setHeader('vertid', verticalId);
     req.setHeader('callback', callbackURL);
     req.setHeader('Connection', 'keep-alive');
     req.setHeader('Content-Length', contentLength);
     req.setHeader('Content-Type', contentType);
     req.setTimeout(120000);
     Http http =new Http();
     HTTPResponse res = http.send(req);
     System.debug('Callout Log 4:' + res.getStatusCode());
     System.debug('Callout Log 5:' + String.valueof(res.getbody()));

     string responseStatusCode = String.valueof(res.getStatusCode()); 
     string responseBody = String.valueof(res.getbody()); 

  }
 }
}
Source Link
Daniel Ballinger
  • 103k
  • 40
  • 275
  • 601

Rather than using EinsteinVision_HttpBodyPart.WriteBodyParameter to append the blob data you need to use EinsteinVision_HttpBodyPart.WriteBlobBodyParameter.

The latter performs extra steps to handle the encoding of the last bytes before the Boundary is added. Note that it will also write it's own boundary.