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:
And in Apex, you can get that file directly via RESTRequest:
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:
And the Apex like this:
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.