I am using this:

Salesforce Apex - POSTing multipart/form-data Content-Type

thread to send a file to a 3rd party

It works great (taken the EinsteinVision_HttpBodyPart class from here)

But when trying to send large files (more than 9MB) I get the StringException: String length exceeds maximum: 12000000

I am using a future method, otherwise I have got a 6000000 limit.

Also - when I try to upload 8MB file I get a heap limit exception: FATAL_ERROR|System.LimitException: Apex heap size too large: 42602311

Is there any way to make send a larger file with this string?

here is the code I have implemented:

public static HttpResponse uploadFile(String endPointUrl, String endPointToken, String fileName, Blob fileBody) {

    // get content type with the boundry
    String contentType = EinsteinVision_HttpBodyPart.GetContentType();

    string fileBodyBase64 = EncodingUtil.base64Encode(fileBody);

    //  Compose the form
    String body = '';
    body += EinsteinVision_HttpBodyPart.WriteBoundary();
    body += EinsteinVision_HttpBodyPart.WriteBodyParameter('name', fileName);
    body += EinsteinVision_HttpBodyPart.WriteBoundary();
    body += EinsteinVision_HttpBodyPart.WriteBlobBodyParameter('data', fileBodyBase64, fileName);
    body += EinsteinVision_HttpBodyPart.WriteBoundary(EinsteinVision_HttpBodyPart.EndingType.CrLf);
    Blob bodyBlob = EinsteinVision_HttpBodyPart.base64Decode(body);

    string contentLength = String.valueOf(bodyBlob.size());

    if(bodyBlob.size()>12000000) {
        // uploadFile: File size limit is 12 MBytes

    HttpRequest req = new HttpRequest();
    req.setHeader('Authorization','Token ' + endPointToken);
    req.setHeader('Content-Length', contentLength);
    req.setHeader('Content-Type', contentType);

    Http httpCall = new Http();
    HttpResponse res = httpCall.send(req);
    return res;

1 Answer 1


Sending large files in multipart HTTP requests is not ideal because the file has to be encoded in a Base64 string and that format creates a huge overhead. Is multi-part a strong requirement of your third party system?

If not, consider sending the file in a binary post, that should help you avoid those string length limitations.

  • I understand, the problem is that I am connecting to 3rd party vendor api and he is not willing to change to a binary post (I have asked), so I am stuck with this Feb 11, 2020 at 7:06
  • Well you won't be able to get rid of that string length limit so you might as well try to reduce the size of file that you're sending if that's possible.
    – POZ
    Feb 11, 2020 at 9:07

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .