Simon Fell > Its just code > Handling binary http response in apex.

Friday, March 02, 2012

Handling binary http response in apex.

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)

< 8:07 PM PST # > tags : Salesforce.com [playing "Close Range" by New Order (from Get Ready)]

You can't post a Comment

Sorry i got sick of comment spam, have something to say about one of my posts, post it to your own weblog.