0

I am trying to launch a callout call to an OCR from an Apex method using a secured named credential with an external credential.

The HTTP request is a POST request with multipart body containing a file. I am using this response to compose the multipart body.

Here a code sample I am using

    ContentDocument document = [SELECT LatestPublishedVersion.VersionData FROM ContentDocument WHERE Id = 'XXXXX'];
    Blob fileContent = document.LatestPublishedVersion.VersionData;
    String contentType = vNHttpFormBuilder.GetContentType();
    String form64 = '';
    form64 += vNHttpFormBuilder.WriteBoundary();
    vNHttpFormBuilder.WriteFileResult result = vNHttpFormBuilder.WriteFile('file', 'test File Name.pdf', 'pdf', fileContent);
    form64 += result.Content;
    form64 += vNHttpFormBuilder.WriteBoundary(result.EndingType);
    Blob formBlob = EncodingUtil.base64Decode(form64);
    String contentLength = String.valueOf(formBlob.size());
    HttpRequest req = new HttpRequest();
    req.setEndpoint('callout:TestCredential/api/publicapi/v1/transactions/launch?skillId=123');
    req.setMethod('POST');
    req.setHeader('Content-Type', contentType);
    req.setHeader('Connection', 'keep-alive');
    req.setHeader('Content-Length', contentLength);
    req.setTimeout(20000);

    req.setBodyAsBlob(formBlob);
    Http http = new Http();
    HttpResponse response = http.send(req);

But I keep getting this error when making the call. Origin of the error is the http.send method. enter image description here

Even though the call seems to work just fine when replacing the endpoint with. req.setEndpoint('https://vantage-eu.abbyy.com/api/publicapi/v1/transactions/launch?skillId=123');

PS: I have tried the named credential (the http call with 'callout:TestCredential') with a GET endpoint and it works fine.

I am suspecting that the script executed by Salesforce to inject the authentication token is failing due to some error with the blob body.

4
  • 1
    Without the actual error message, it's going to be hard to help you. Try wrapping the entire thing in a try-catch block and see what the exception message is.
    – sfdcfox
    Commented Sep 21, 2023 at 12:58
  • Hi @sfdcfox thank you for your comment. That's actually the issue I don't have much information. Even with a try catch I still get the same error. It says that the error is a CalloutException: script thrown exception originating from the http.send method.
    – Diaboloxx
    Commented Sep 22, 2023 at 7:53
  • Take a look here mate : help.salesforce.com/s/articleView?id=000393013&type=1 It may help. It did for me.... Commented Aug 13 at 0:09
  • and yes, that article specifically mentions AWS, but the setting of the 'Content-Length' header on non AWS endpoints also caused this script error.....(well, it did for me anyway) ... Hope that helps...... Commented Aug 13 at 1:07

0

You must log in to answer this question.