I'm working on part of an Apex class to store a retrieved binary file using the REST API to a test folder on Salesforce and I'm not sure exactly how it'll work. I have the Salesforce ID of the test folder, but I'm not sure if it'll require an HTTP POST like this one below:

   public void basicCallout(){
     HttpRequest req = new HttpRequest();
     req.setHeader("Authorization", "Oauth" + loginResult.getAccessToken());
     HTTPResponse res = new Http().send(req);

I'm working off the following example as a reference:


Since you're already in Apex Code, you can store the document directly:

Document doc = new Document(Name=..., Body=res.getBodyAsBlob(), FolderId=...);
insert doc;

As a side node, if you did need a second callout, you'd need to create a new HttpRequest, and not modify the HttpResponse res as you've done here.

  • I see. I'll keep that in mind. Out of curiosity, is there any way to preserve the filename and extension when it's transferred over? To my understanding, HTTPRequest/HTTPResponse commands can only transfer the binary data and not anything else. – xorLogic May 24 '18 at 14:38
  • @xorLogic you're right, it's just the binary data. You can use the URL class to decompose the original URL you're calling, but I'm not sure if that's what you're after. – sfdcfox May 24 '18 at 15:30
  • That might work. I'm looking to transfer files between Salesforce and O365. I don't need to preserve the metadata of the files, just the filenames themselves to reference their originals. – xorLogic May 24 '18 at 20:28

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.