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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With