[SalesForce] Problem trying to upload ContentVersion from base64 image data via REST API

I am trying to upload a small image in base64 as a Chatter file (ContentVersion record where Origin = 'H') via the Force.com REST API.

Here are the headers and body that I am sending:
https://gist.github.com/lukemcfarlane/4ab87a67e6bf3ef9eb83

I am testing this using a Mac REST client called RESTed (https://github.com/hellopatrick/rested), and I am getting back a successful response:

HTTP/1.1 201 Created

{
    "id": "068R0000000Dk7lIAC",
    "success": true,
    "errors": []
}

I then find the file under Chatter > Files in Salesforce and it exists, however when I download and try to open the file I get an error message saying that the file is damaged and cannot be opened.

I am closely following the example in the following documentation:
http://www.salesforce.com/us/developer/docs/api_rest/index_Left.htm#CSHID=dome_sobject_insert_update_blob.htm|StartTopic=Content%2Fdome_sobject_insert_update_blob.htm|SkinName=webhelp

One question I have is, where it says "Updated document binary data goes here." should that be base64 encoded binary data? If so then what else could be going wrong?

Update

I opened the "damaged" file with a text editor on my local machine and observed that the file consists of the original base64 data that I uploaded. This tells me that the API must not be decoding my base64 into a binary file.

Best Answer

I eventually ruled out being able to use base64, this is because the API does not seem to support the "Content-Transfer-Encoding: base64" header.

Further to this, when I tried sending raw binary data I had a problem where RESTed (the native Mac REST client that I was using) was escaping all of the UTF8 characters before sending the request.

When I used curl with the --data-binary flag the image uploaded and worked with no problems. I am kicking myself now because the examples in the documentation all mentioned using curl!

Thanks for all of those people who tried to help!

Related Topic