1

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.setEndpoint(endPoint);
        req.setTimeout(TIMEOUT);
        req.setMethod('PUT');
        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);
        }

        req.setBodyAsBlob(fileBlob);

        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;
        }
    }
3
  • @identigral - lol - that is what I have suggested to the Team Lead might be a solution ;)
    – Tony White
    Commented Jan 27, 2022 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
    Commented Jan 27, 2022 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
    Commented Jan 27, 2022 at 4:54

1 Answer 1

3

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.

1
  • Same for me when adding an item to a GCloud bucket using this API: cloud.google.com/storage/docs/json_api/v1/objects/insert Fortunately confirmed with Postman that the Content-Length doesn't have to actually bear any resemblance to the length of the upload when doing a simple, non-multipart, non-resumable upload, so removed that header from the Apex HttpRequest, and all is well
    – dyson
    Commented Jun 20 at 23:16

You must log in to answer this question.

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