Sending Binary Data To Apex (Updated)

I previously posted about sending binary data to Apex using Apex REST as the example.  In that example I used base64 encoding to transfer data, but as it turns out that should not be necessary when transmitting your data via POST, PUT or PATCH.  The RESTRequest object is designed to consume binary data directly and be accessed via the RESTRequest’s requestBody without modification.  So, in node using the FDC-HEROKU-NODE project, you can use this to read a file off the local system:

data = fs.readFileSync('test.png');
rest.execute('FieldCase?companyName=blah','PATCH',data,null,console.log);

And in Apex, you can get that file directly via RESTRequest:

@HttpPut
  global static String updateCase(RestRequest req) {
      String companyName = req.params.get('companyName');
      Account company = [ Select ID, Name, Email__c, BillingState from Account where Name = :companyName];
      Attachment a = new Attachment();
      a.ParentId = company.Id;
      a.Name = filename;
      a.Body = req.requestBody;
      insert a;
      return 'Attachment added';
  }

 

The trick in node, for the watchful, is not set an encoding – which leaves it as a binary buffer.  Setting anything as a second option with readFileSync will attempt to convert it to a string (even if that option is “binary”).

If you were still using SOAP, you would need to encode the data in base64 before sending it.  Base64 allows binary data to be transmitted safely as ASCII, but also increases the size of information that you are sending across the wire.  So for example, if I wanted to take a snapshot of UI component in Flex using the SOAP toolkit and attach it to a company, the ActionScript would look like this:

public function sendScreenshot():void {
  var snapshot:ImageSnapshot = ImageSnapshot.captureImage(resultGrid);
  var b64String:String = ImageSnapshot.encodeImageAsBase64(snapshot);
  var p1:Parameter = new Parameter("companyName","GenePoint");
  var p2:Parameter = new Parameter("img",b64String); 
  this.conn.execute('RESTCaseController','addAttachment',[p1,p2], new com.salesforce.AsyncResponder( com.force.utility.util.genericError,   com.force.utility.util.genericError));
}

And the Apex like this:

webService static String addAttachment(String companyName, String img) {
      Account company = [ Select ID, Name, Email__c, BillingState from Account where Name = :companyName;

      Attachment a = new Attachment();
      a.ParentId = company.Id;
      a.Name = 'testfromflex.png';
      a.Body = EncodingUtil.base64Decode(img);

      insert a;

      return 'Attachment added';
    }

To see more Apex REST code like the above, and learn how you can get your hands on it – check out the upcoming Apex REST webinar on July 19th, and of course to learn more about juggling Apex, node, Flex and bits of binary in general – no better place than Dreamforce.  Also, look for my session on “Data Visualizations With Flex” for more tricks the like one above on merging your data with Flex and Force.com.

 

Published
July 15, 2011
Topics:

Leave your comments...

Sending Binary Data To Apex (Updated)