Simon Fell > Its just code > Handling binary http response in apex.
Friday, March 2, 2012
For quite a while now you've been able to make HTTP requests from Apex to other services, this was aimed at integrations for structured data, xml or json, and so would deal with strings. This made life easier if you were actually doing xml or json, but makes life difficult to impossible if you were trying to deal with binary data. In the recent Spring release this is fixed, and 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
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);
(Yes, this may well be my one blog post for this year)