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

How to return binary data from an External web service callout (HttpRequest)?

Can anyone tell me if  it is possible to get binary data back from a web service callout?


I am using the apex Http, HttpRequest and HttpResponse classes to call an external  service that returns a binary file. I then attach that file to an email programatically. Everything works fine, except that my binary data in the attachment is mangled, because of the string conversion on response.getBody(). Is there a way to retrieve the binary stream from the HttpResponse class without corrupting it? 


Or is there another set of classes I should use to perform this function to get the results I'm looking for? 


Thanks for any help!





is this possible?


AFAIK, you can't handle binary data correctly with the callout classes.


Did you ever get this to work? 


If so how?


I pass base64 format data and decode it in apex.


...I pass base64 format data and decode it in apex.


can you show me how please?


I found a link that knows how to do this...


In the recent Spring release (v24) ... you can now work with binary data (blobs in apex) directly in the http request or response. Here's an example of making a HTTP GET request for an image PNG file, and saving it to the document object in salesforce.


Here is the code in that link:


HttpRequest r = new HttpRequest();
Http http = new Http();
HttpResponse res = http.send(r);
blob image = res.getBodyAsBlob();

Document d = new Document(); = 'logo.png';
d.body = image;
d.folderId = UserInfo.getUserId();
insert d;