Avdept
Avdept

Reputation: 2289

Upload file with api and carrierwave

I'm writting api for webservice, and i need to provide ability for users to upload files using this api.

For my uploads i'm using carrierwave, but not sure how do i pass file from api request to carrierwave, and how the file should be sent from client machine to server

Upvotes: 3

Views: 3035

Answers (3)

Lane
Lane

Reputation: 4996

Try RestClient. It encapsulates net/http with cool features like multipart form data:

require 'rest_client'
RestClient.post('http://localhost:3000/foo', 
  :name_of_file_param => File.new('/path/to/file'))

It also supports streaming.

gem install rest-client will get you started.

Upvotes: 0

Mohanraj
Mohanraj

Reputation: 4200

Basically it is based on the tool which is used by the API consumer. If API consumer is using ruby then can consume it by passing File object, or using httmultiparty gem we can upload the file.

For you reference https://github.com/jwagener/httmultiparty. Please let me know if you need more help.

Upvotes: 1

jonfaulkenberry
jonfaulkenberry

Reputation: 96

Usually I implement file uploads over a REST API by allowing clients to send a PUT request with the base64 encoded binary data of the file inside the request body.

Then you can route the client request to your CarrierWave uploader, which can decode the binary data contained in the request body using something like FilelessIO.new(Base64.decode64(encoded_file))

Upvotes: 1

Related Questions