4

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.

  • I believe you're right, but you'd have to look at the data you download to confirm. Have you tried comparing using diff in binary mode or some other tool? – sfdcfox Jul 23 '14 at 4:58
  • To remove doubt, you can also add a "Content-Transfer-Encoding: base64" header but as the examples don't mention that it is probably assumed. – Keith C Jul 23 '14 at 8:50
  • When I added this header I got "HTTP/1.1 500 Internal Server Error". – luke.mcfarlane Jul 23 '14 at 21:30
2

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!

0

Everything looks right but the data you are sending should be like

Filedata.split(',')[1]

This will send only base64 data of your file.

try it and let me know if you found any problem.

You can also use SOAP API for upload as it is very simple.

Thanks

0

The base64 data is in GIF format; changing "image.png" to "image.gif" so that the correct content type is inferred is likely to fix the problem. You can query the resulting Attachment SObject.ContentType field to check.

  • I tried changing both the filename and PathOnClient value to "image.gif", however this made no difference. I also tried changing the Content-Type header for the binary part to "image/gif" but still no luck. – luke.mcfarlane Jul 23 '14 at 20:50
  • @luke.mcfarlane Bad news then. If you have the original binary file you can compare its size with Attachment.BodyLength to get an indication if the bytes made it through the whole process intact. Is the Attachment.ContentType correct i.e. image/gif? – Keith C Jul 23 '14 at 21:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.