We have a method that puts files in an AWS S3 Bucket, but since Spring 22 was applied to an env it has been failing with 'System.CalloutException: Script-thrown exception'

We believe it to be related to this Known-Issue: https://trailblazer.salesforce.com/issues_view?id=a1p4V000001JitQQAS&title=script-thrown-exception-in-apex-callouts

The workaround suggested is about content encoding of the response, but that is not something we can control with the AWS S3 bucket response to my knowledge.

Is there ANYTHING we can do within the Salesforce code to fix this issue?

    public Boolean putFile(String filename, String contentType, Blob fileBlob) {
        HttpRequest req = new HttpRequest();
        String endPoint = 'callout:AWSS3Bucket/' + filename;
        req.setHeader('Connection', 'keep-alive');
        req.setHeader('Content-Length', String.valueOf(EncodingUtil.base64Encode(fileBlob).length()));
        req.setHeader('Content-Encoding', 'UTF-8');

        if (String.isEmpty(contentType)) {
            // AwS Defaults to CONTENT_TYPE_BINARY if not specified, setting explicitly to avoid confusion
            req.setHeader('Content-Type', CONTENT_TYPE_BINARY);
        } else {
            req.setHeader('Content-Type', contentType);


        Http http = new Http();
        HttpResponse resp = http.send(req);

        if (resp != null && resp.getStatusCode() == 200) {
            return true;
        } else {
            System.debug('Status: ' + resp.getStatus());
            System.debug('Body: ' + JSON.serializePretty(resp.getBody()));
            return false;
  • @identigral - lol - that is what I have suggested to the Team Lead might be a solution ;)
    – Tony White
    Jan 27 at 4:30
  • Interestingly it does work sometimes on Spring22, thought it was before 10am (being in GMT+10) but found 1 test that worked at 10:27am Will test again tomorrow morning and see what happens
    – Tony White
    Jan 27 at 4:41
  • Ok further trial and error we did find something that does appear to work.... do not set the Content-Length
    – Tony White
    Jan 27 at 4:54

1 Answer 1


Ok interesting for this particular method we have resolved the issue by NOT setting the Content-Length header.

As soon as we stopped setting that, we stopped getting the Callout Exception.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.