Pages

Showing posts with label API. Show all posts
Showing posts with label API. Show all posts

Thursday, January 30, 2014

API Upload with CarrierWave

 I want to share a quick snippet with regards to a problem I had a few months ago: How to upload a picture/file to a Ruby on Rails project, which is utilizing CarrierWave to handle the upload mechanism, by utilizing the direct POST call.
With this comes at least two challenges:
  • You cannot insert a file directly into a JSON object;
  • CarrierWave does not support uploading a file which is not uploaded using a form object.
Solving the first problem is almost trivial, you simply encode the file into Base64 and pass it along, but the second problem requires a little more work. What I did to solve it was to decode the Base64 string into a temp file, and then create a new uploaded file from that temp file.