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
Rakesh ERakesh E 

Posting PDF file in Document object using REST API

Hi,

 

i am trying to post/create a new pdf Document record in salesforce Document object from one org to other using REST API.

i am querying the BLOB data from one Document record from soure org and preparing a POST request and trying to post it in other org .

i am using EncodingUtil.base64Encode(Sourcedocument.body) to send the pdf file.

 

but this file is not getting saved correctly and unabel to open the PDF (getting error : File not start with %PDF). its getting corrupted

 

what is the mistake i am doing please let me know. is the way i am sending the binary data is wrong ?

please let me know if anyone have idea about this

 

Thank you

 

Regards,

Rakesh

SaraagSaraag

I think attachment/document body is already base64. Could you do something like this?

 

    Http h = new Http();
    HttpRequest req = new HttpRequest();
    //set authorization header and url
    req.setMethod('POST');
    req.setHeader('Content-Type', 'application/json');
    // get document 'a'
    req.setBodyAsBlob(a.body);
    // Send the request, and return a response
    HttpResponse res = h.send(req);
    system.debug(logginglevel.error, res.getBody());

 

Saraag