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?


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.

链接地址: http://www.djcxy.com/p/60606.html

上一篇: .NET和PHP加密的区别

下一篇: 如何通过Carrierwave通过JSON API上传文件?