6

I have an application built in ruby on rails. I need to post curl request to that application to upload images from local machine. In my ruby on rails application I am using paperclip for image uploader.

Now this curl request is working perfectly fine as there is no upload image for this curl request:

  curl -v -H   "Accept: application/json" -H "Content-type: application/json" -X PUT -d '{"user":{"first_name":"John","last_name":"Smith"},"token":"api"}'  http://localhost:3000/users/8

Now I want to upload an image from my localmachine.

Adding "display_photo":@test.jpg is not working: This is what I was trying: (But its not working.)

  curl -v -H  -F "Accept: application/json" -H "Content-type: application/json" -X PUT -d '{"user":{"first_name":"firstname","last_name":"lastname","display_photo":@test.jpg},"token":"api"}'  http://localhost:3000/users/8

So the question is how to upload images/data files/ videos from curl request

EDIT

This is my controller:

   def update
    @user = User.find(params[:id])

    respond_to do |format|
      if @user.update_attributes(params[:user])
        format.html {  redirect_to(@user, :notice => 'User was successfully updated.') }
        format.json  { render :file => "users/json/show.json.erb", :content_type => 'application/json' }
      else
        format.html { render :action => "edit" }
        format.json  { 
          @errors_object = @user
          render :file => "shared/json/errors.json.erb", :content_type => 'application/json' }
      end
    end
  end

In my model:

class User < ActiveRecord::Base
    has_attached_file :display_photo, :styles => { :medium => "200x200#", :thumb => "100x100#", :very_small=>"24x24#" }
end
Mohit Jain
  • 43,139
  • 57
  • 169
  • 274
  • do you want to upload your photo inline?! – phoet Jul 09 '12 at 19:18
  • @phoet inline what? its a request to update your profile. I want to update user's profile image. – Mohit Jain Jul 09 '12 at 19:26
  • yeah, but you are trying to put a file from your local machine into a json string. or do i misunderstand your curl command? – phoet Jul 09 '12 at 20:18
  • @phoet I want to upload a file, I am not able to figure out how to pass that file argument. Second curl is wrong, i need help in that only. that how to pass local image in curl request. – Mohit Jain Jul 10 '12 at 07:28

3 Answers3

14

you would normally upload a file like this:

curl -i -F filedata=@upload.txt http://localhost:5000/

doing so mimics the behavior that you would have through a webbrowsers file upload.

if you look at the rails response for this, it's sent via application/octet-stream that can be handled properly as an uploaded file. as far as i know it's not possible to do this with a json request:

Started POST "/" for 127.0.0.1 at 2012-07-10 09:40:59 +0200
Processing by HomeController#index as */*
  Parameters: {"filedata"=>#<ActionDispatch::Http::UploadedFile:0x007f82d59a3580 @original_filename="upload.txt", @content_type="application/octet-stream", @headers="Content-Disposition: form-data; name=\"filedata\"; filename=\"upload.txt\"\r\nContent-Type: application/octet-stream\r\n", @tempfile=#<File:/var/folders/f_/wjnrg7cd3d9f1tpy3k5fhrwm0000gn/T/RackMultipart20120710-30471-1t9abns>>}
phoet
  • 18,688
  • 4
  • 46
  • 74
  • take a look on my edit part. This is how I am accepting my requests. I have removed the api_token check from the code. How can you help me in passing attributes like first_name, last_name and a display image using only one call. – Mohit Jain Jul 10 '12 at 08:17
  • have a look at this example http://leejava.wordpress.com/2009/07/30/upload-file-from-rest-in-ruy-on-rail-with-json-format/ – phoet Jul 10 '12 at 08:24
  • Keep in mind "filedata" part is the name of the actual file upload which is what you will use to reference in the server side. – cevaris Jan 24 '14 at 03:08
6

I had the same problem and I solved it with the following, which bypass the JSON request:

curl -v -H 'Content-Type: multipart/form-data' -H 'Accept: application/json' -H 'x-api-key:myapikey' -F "user[first_name]=firstname" -F "user[last_name]=lastname" -F "user[display_photo]=@test.jpg" http://localhost:3000/users/8 
r4m
  • 491
  • 4
  • 14
0
curl -v -include --form "<param>=@/<root>/image_name.png" http://<your url>
**<param>**: The image parameter of the url;
**<root>**: You can use pwd get image root;
**<your url>**: Post url;
Codios
  • 378
  • 4
  • 11
  • Please consider editing your post to add more explanation about what your code does and why it will solve the problem. An answer that mostly just contains code (even if it's working) usually wont help the OP to understand their problem. – Drenmi Dec 08 '15 at 07:55