I am trying to send a http request with a large string Body; however, salesforce complaints about the size of the string, the error as follows:

caused by: System.StringException: String length exceeds maximum: 12000000

The http request as follows:

Http http = new Http();
HttpRequest request = new HttpRequest();
request.setHeader('Content-Type', 'application/x-www-form-urlencoded');
String payload = 'client_id=' + EncodingUtil.urlEncode('39494342', 'UTF-8') + '&file=' + EncodingUtil.urlEncode(getBase64File(), 'UTF-8');
HttpResponse response = http.send(request);

static String getBase64File() {
    String result;
    // to some stuff
    // at this stage the result contains 12 million characters

    return result;

The method that causes the issue is getBase64File, this method generates a huge string of about 13 million characters. Is there any workaround for this issue so that I can send the payload?

  • 2
    There's no way around the string exception, that's a Salesforce limitation. What is it that you're trying to send? At 13 million characters, you're around 10 MB of data. – nbrown Dec 9 '19 at 16:56
  • 2
    base64 makes things take up more characters (1.333x the original, plus up to 2 additional padding bytes). I don't suppose that reducing the dimensions of the image, or compressing it is an option, is it? – Derek F Dec 9 '19 at 18:05
  • 3
    Salesforce does not put and hard limit on Strings it comes from heap size if you can do it asynchronously then you may get some extra heap size. String size: Strings have no limit on the number of characters they can include. Instead, the heap size limit is used to ensure that your Apex programs don't grow too large. – Avijit Chakraborty Dec 9 '19 at 19:33
  • 2
    You could compress the images before uploading them to Salesforce. – Derek F Dec 10 '19 at 12:06
  • 1
    You could modify your workflow so that users within Salesforce are uploading images to your compression server and, when finished, have the compression service make a request to Salesforce to send it into your org. – nbrown Dec 10 '19 at 13:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.