Curl请求将映像从本地计算机上载到rails应用程序上的ruby

我有一个在轨道上用ruby构建的应用程序。 我需要将curl请求发布到该应用程序以从本地计算机上传图像。 在我的ruby on rails应用程序中,我使用paperclip进行图像上传。

现在这个curl请求工作得很好,因为这个curl请求没有上传图像:

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 

现在我想从本地机器上传图像。

添加“display_photo”:@ test.jpg无法正常工作:这就是我的尝试:( 但它不起作用 。)

  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 

所以问题是如何从curl请求上传图像/数据文件/video

编辑

这是我的控制器:

  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 

在我的模型中:

 class User  { :medium => "200x200#", :thumb => "100x100#", :very_small=>"24x24#" } end 

你通常会上传这样一个文件:

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

这样做会模仿您通过webbrowsers文件上传所具有的行为。

如果您查看rails响应,则通过application/octet-stream发送,可以作为上传文件正确处理。 据我所知,用json请求不可能这样做:

 Started POST "/" for 127.0.0.1 at 2012-07-10 09:40:59 +0200 Processing by HomeController#index as */* Parameters: {"filedata"=>#>} 

我有同样的问题,我解决了以下问题,绕过了JSON请求:

 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 
 curl -v -include --form "=@//image_name.png" http:// ****: The image parameter of the url; ****: You can use pwd get image root; ****: Post url;