Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I upload a file via Carrierwave over a JSON API?

I'm building a file-manipulation API in Rails, and I need to be able to access it via a separate gem. The API uses Carrierwave, and that piece works with no trouble. What I don't understand how to do is to take an arbitrary file and deliver it to the API from the gem interface.

Carrierwave takes its files as either the results of File.open('foo.jpg') or as a POST from a file field. I'm really not sure what either is doing, though, to serialize the file and send it along.

How do I take the contents of a file and turn that into something I can pass around and post via JSON?

like image 371
Mark Tabler Avatar asked Jun 12 '12 23:06

Mark Tabler


1 Answers

When an HTML form POSTs a file, what actually happens is a special part of HTTP called a multipart request. Effectively, the file gets "attached" to the request.

The question will be answered by what library you are using to POST the JSON to your api. Attaching a file to a request should be fairly commonplace but not all libraries may support it.

This stack overflow article seems to give some good indications of how to do it.

like image 53
Daniel Evans Avatar answered Sep 21 '22 12:09

Daniel Evans