2012-07-09 53 views
10

多形式的數據我知道如何使用基本身份驗證與Ruby的rest-client紅寶石其餘的客戶端文件上傳與基本authenticaion

response = RestClient::Request.new(:method => :get, :url => @base_url + path, :user => @sid, :password => @token).execute 

以及如何發佈文件作爲多形式的數據來做出一個HTTP請求

RestClient.post '/data', :myfile => File.new("/path/to/image.jpg", 'rb') 

但我似乎無法弄清楚如何將兩者結合以便將文件發佈到需要基本身份驗證的服務器。有誰知道創建此請求的最佳方式是什麼?

+1

關於不相關的說明:您應該接受更多答案,其良好實踐... – robustus 2012-07-09 11:20:50

回答

20

如何使用RestClient::PayloadRestClient::Request ... 舉一個例子:

request = RestClient::Request.new(
      :method => :post, 
      :url => '/data', 
      :user => @sid, 
      :password => @token, 
      :payload => { 
      :multipart => true, 
      :file => File.new("/path/to/image.jpg", 'rb') 
      })  
response = request.execute 
0

最新最好的方法可能是: 鏈路enter link description here

RestClient.post(url, 
    { 
    :transfer => { 
     :path => '/foo/bar', 
     :owner => 'that_guy', 
     :group => 'those_guys' 
    }, 
    :upload => { 
     :file => File.new(path, 'rb') 
    } 
    }) 
0

下面是一個例子用一個文件和一些json數據:

require 'rest-client' 

payload = { 
    :multipart => true, 
    :file => File.new('/path/to/file', 'rb'), 
    :data => {foo: {bar: true}}.to_json 
     } 

r = RestClient.post(url, payload, :authorization => token) 
0

RestClient API似乎已經改變。以下是使用基本身份驗證上傳文件的最新方式:

response = RestClient::Request.execute(
    method: :post, 
    url: url, 
    user: 'username', 
    password: 'password', 
    timeout: 600, # Optional 
    payload: { 
    multipart: true, 
    file: File.new('/path/to/file, 'rb') 
    } 
)