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
twixtwix 

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!

Gretchen

 

 

TheMythicalTheMythical

is this possible?

SuperfellSuperfell

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

TomSnyderTomSnyder

Did you ever get this to work? 

 

If so how?

TheMythicalTheMythical

I pass base64 format data and decode it in apex.

alberto.furlani@bitmovers.italberto.furlani@bitmovers.it

@TheMithical

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

 

can you show me how please?

TerryLuschenTerryLuschen

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

http://stackoverflow.com/questions/9632570/upload-image-from-android-client-to-salesforce-custom-object

 

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();
r.setMethod('GET');
r.setEndpoint('http://www.pocketsoap.com/osx/soqlx/soqlxicon.png');
Http http = new Http();
HttpResponse res = http.send(r);
blob image = res.getBodyAsBlob();

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