function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
George Laird 29George Laird 29 

Please help!!! Need to pass PDF attachment as body in callout! Stuck!!!

Please!  I'm so stuck.  I am making a callout where the body is simply a PDF attachment.  This is working correctly in Postman, but I can't get it to work in my APEX callout.  

Here is a screen shot from postman.  The body only has to be the key 'file' and the .pdf attachment. 

User-added image


I can't get it to work.  Here is my code:

 public static void sendPDF(string collectionID, id intakeID){
        
        system.debug('You are now in the sendPDF method and the resource key is' +collectionID);
       
        // Get the attachment to pass 
        Attachment objPDF = [SELECT ID,ParentID,Name,Body FROM Attachment WHERE ParentID =: intakeID LIMIT 1]; 
        system.debug('The ID of the Attachment is '+objPDF);
        
        // change the following variables according to your use-case
        String strParserId = 'PARSERID';
        String strSeparationKey = 'A_RANDOM_STRING';

        // assemble the body payload
        String strHeader = '--' + strSeparationKey + '\nContent-Disposition: form-data; name="File"; filename="' + objPDF.Name + '"\nContent-Type: application/octet-stream\n\n';
        String strBody = EncodingUtil.base64Encode(objPDF.Body);
        String strFooter = '\n--' + strSeparationKey + '--';

        String strHeaderEncoded = EncodingUtil.base64Encode(Blob.valueOf(strHeader+'\n'));
        while(strHeaderEncoded.endsWith('=')) {
            strHeader+=' ';
            strHeaderEncoded = EncodingUtil.base64Encode(Blob.valueOf(strHeader+'\n'));
        }
        
        String strBodyEncoded = strBody;
        String strFooterEncoded = EncodingUtil.base64Encode(Blob.valueOf(strFooter));

        Blob blobBody = null;
        String last4Bytes = strBodyEncoded.substring(strBodyEncoded.length()-4,strBodyEncoded.length());

        if(last4Bytes.endsWith('=')) {
            Blob decoded4Bytes = EncodingUtil.base64Decode(last4Bytes);
            HttpRequest objHttpRequest = New HttpRequest();
            objHttpRequest.setBodyAsBlob(decoded4Bytes);
            String last4BytesFooter = objHttpRequest.getBody()+strFooter;
            blobBody = EncodingUtil.base64Decode(strHeaderEncoded+strBodyEncoded.substring(0,strBodyEncoded.length()-4)+EncodingUtil.base64Encode(Blob.valueOf(last4BytesFooter)));
        } else {
            blobBody = EncodingUtil.base64Decode(strHeaderEncoded+strBodyEncoded+strFooterEncoded);
        }

        if(blobBody.size()>3000000) {
            // throw new CustomException('File size limit is 3 MBytes');
            system.debug('File size limit is 3 MBytes');
        }else{
            system.debug('blobBody.size()'+blobBody.size());
            system.debug('The sent blob body: '+blobBody);
        }

        // send out the request
        HttpRequest req = New HttpRequest();
        req.setHeader('Content-Type', 'multipart/form-data; boundary=' + strSeparationKey);
        req.setMethod('POST');
        req.setEndpoint('callout:Omnius/collections/'+collectionID +'/documents');
        req.setBodyAsBlob(blobBody);
        Http http = New Http();
        HTTPResponse res = http.send(req);
        system.debug('res'+res.getBody());
    }   
        
    }



Can anyone tell me what I am doing wrong?   I think the problem is the body and that's all.  As I said, it's working in Postman correctly.  

Any help at all would be awesome!