For learning purpose, I am trying to upload a PDF through a REST api. I tried to write apex code for the same,

I am able to insert/upload pdf but the PDF is corrupted.

I am unable to open the same inserted Pdf.

Reference for the code: https://docparser.com/blog/post-file-salesforce-apex-external-http-webservices/

Salesforce Documentation link : https://developer.salesforce.com/docs/atlas.en-us.api_rest.meta/api_rest/dome_sobject_insert_update_blob.htm

Code for the same:

public static void sendPdfRequest(){
    // change the following variables according to your use-case
    String fileName = 'MY_FILE.pdf';
    String fileContent = 'Hello World';
    String targetURL = 'https://lightning-my-domain-enabled.my.salesforce.com/services/data/v43.0/sobjects/Document';
    String separationString = 'abc';

    // assemble the body payload
    String preHeader = '--' + separationString 
                    +'\nContent-Disposition: form-data; name="entity_content";\n'
                    +'Content-Type: application/json\n\n'
                    +'"Description" : "Marketing brochure for Q1 2011",\n'
                    +'"FolderId" : "00l280000013KuIAAU",\n'
                    +'"Name" : "Marketing Brochure Q1",\n'
                    +'"Type" : "pdf"\n'

    String header = '--' + separationString 
                    + '\nContent-Disposition: form-data; name=\"Body\"; filename=\"' + fileName 
                    + '"\nContent-Type: application/pdf\n\n';
    System.debug('headerrrr:' + header);
    String body = EncodingUtil.base64Encode(Blob.valueOf(fileContent) ) + '\n';
    System.debug('Bodyyyyy: '  + body);
    String footer = '--' + separationString + '--';
    System.debug('footerrr:'  + footer);
    String bodyPayload = preHeader + header + body + footer;
    System.debug('bodyPayloadddd ::' + bodyPayload);

    // send out the request
    HttpRequest req = new HttpRequest();
    req.setHeader('Authorization' , 'Bearer ' + <TOKEN>);
    req.setHeader('Content-Type', 'multipart/form-data; boundary=' + separationString);
    req.setHeader('Content-Length', ''+ (1 * String.valueof(bodyPayload).length() ) );

    Http http = new Http();
    HttpResponse  hr =  http.send(req);
    System.debug('Response :: ' + hr.getBody());


Let me know which part is erroneous so that I can correct it.

  • Apex handles building binary files very poorly in my experience. Might be worth looking into building a page, using displayAs="pdf", and creating a file with the body of that file being set to the result of new PageReference(PdfPage).GetContentasPDF() – battery.cord Oct 17 '18 at 13:43
  • yes those are the some of the alternate ways. However, I was wondering if I could do this with SF's Document's REST api(considering a hypothetical scenario, where I might need to upload document in SF from another technology, by means of their REST api). – Ysr Shk Oct 19 '18 at 7:39

Your Answer

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

Browse other questions tagged or ask your own question.