0
List<ContentVersion> cvList = [SELECT VersionData, Title FROM ContentVersion WHERE Id = '068OD00000137hmYAA'];        
String boundary = '----WebKitFormBoundary' + String.valueOf(Crypto.getRandomLong());

String requestUrl = 'https://api.in1.adobesign.com:443/api/rest/v6/transientDocuments';
        
        String header = '--' + boundary + '\r\n' + 'Content-Disposition: form-data; name="File"; filename="{0}"\r\nMime-Type: {1}\r\n\r\n';
        String footer = '\r\n--' + boundary + '--\r\n';
        List<String> parts = new List<String>();
        for (ContentVersion thisVersion : cvList) {
            String contentType = 'application/pdf'; // Replace with the correct content type based on the file type
            String part = String.format(header, new String[]{thisVersion.Title, contentType}) + EncodingUtil.base64Encode(thisVersion.VersionData).replaceAll('[/+]', '_').replaceAll('[-]', '/') + footer; //thisVersion.VersionData + footer; //
            parts.add(part);
        }
        String requestBody = String.join(parts, '');
        Blob requestBlob = Blob.valueOf(requestBody);
        HttpRequest request = new HttpRequest();
        request.setEndpoint(requestUrl);
        request.setMethod('POST');
        request.setHeader('Authorization','Bearer '+accessToken);
        request.setHeader('Content-Type', 'multipart/form-data; boundary=' + boundary);
        request.setBodyAsBlob(requestBlob);
        Http http = new Http();
        HttpResponse response = http.send(request);
        system.debug('Transient Document==>'+response.getBody());
        if(response.getStatusCode()==200){
            String responseBody = response.getBody();
            system.debug(VA_TransientDocumentJSONWrapper.parse(responseBody));
            VA_TransientDocumentJSONWrapper wrapper  = VA_TransientDocumentJSONWrapper.parse(responseBody);
            system.debug('wrapper'+wrapper.transientDocumentId);
            return wrapper.transientDocumentId;
        }

I'm able to get the transient documentId as a response and agreement is also generated and email is received for e-signature. However, attached pdf in the email is base64 encoded and does not show the actual PDF. Kindly let me know what I'm missing. Need urgent help.!!

1 Answer 1

0

I was having a similar issue when creating a widget with a transientDocumentId, and it showed the document contents as a base64 string of text.

This is because EncodingUtil.base64Encode takes a Blob of bytes and converts them into a base64 string of text. This means when you send this string in the request body of the HTTP REST callout to /transientDocuments, you are not sending base64 bytes. Instead you are sending ASCII characters that represent the base64 encoding.

Although it is unrelated to Salesforce and Apex, I found this Stack Overflow post that gave a hint to solve this issue. We must specify Content-Transfer-Encoding: base64 on a separate line to tell the server how the file contents are encoded.

Also, I changed Mime-Type: to Content-Type: in the request body. Not sure if this change is required or not, but it works. So basically I make the request body look something like this:

--<some_boundary>
Content-Disposition: form-data; name="File"; filename="<filename>.pdf"
Content-Type: application/pdf
Content-Transfer-Encoding: base64

<file_contents_in_base64>
--<some_boundary>--

Hope this helps!

1
  • 1
    You are the saviour.! Although initially, I did try using Content-Transfer-Encoding: base64, but it didn't work. Maybe, I was not using it correctly. But looking at your answer and with minor modifications, I'm able to get the actual PDF content. Almost spent 2 days trying to generate correct requestbody. Thank you so much for your help. Commented May 30 at 15:00

You must log in to answer this question.

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