2011-11-11 18 views
3

我無法翻譯使用RESTClient實現這種捲曲請求轉換成紅寶石:重新分解捲曲請求Ruby的RESTClient實現

system("curl --digest -u #{@user}:#{@pass} '#{@endpoint}/#{id}' --form [email protected]'#{path}' -X PUT") 

我不斷收到400 Bad Request錯誤。據我所知,請求確實得到了正確的認證,但是從文件上傳部分掛起。以下是我的最佳嘗試,所有這些都會給我帶來400個錯誤:

resource = RestClient::Resource.new "#{@endpoint}/#{id}", @user, @pass 
#attempt 1 
resource.put :image_file => File.new(path, 'rb'), :content_type => 'image/jpg' 
#attempt 2 
resource.put File.read(path), :content_type => 'image/jpg' 
#attempt 3 
resource.put File.open(path) {|f| f.read}, :content_type => 'image/jpg' 
+0

您不應將content_typa作爲image/jpg傳遞,因爲您正在提交已編碼的表單。我會簡單地嘗試像這樣'resource.put:image_file => File.new(path,'rb')'也看到這個stackoverflow後http://stackoverflow.com/questions/184178/ruby-how-to-通過http-as-multipart-form-data –

+0

刪除content_type不會改變任何內容。我認爲這可能與提供的路徑有關。給出的路徑是一個在CURL中正常工作的絕對路徑。我怎樣才能讓File.read處理絕對路徑? – user94154

回答

2

您通過PUT請求發送多形式的數據,那麼相應地就需要做同樣的RESTClient實現:

resource = RestClient::Resource.new "#{@endpoint}/#{id}", @user, @pass 
resource.put :image_file => File.new(path, 'rb'), :content_type => 'multipart/form-data', :multipart => true 
1

粗壯是正確的,你也可以使用RESTClient實現::有效載荷::多部分。

但是我已經看到,你問這是你的Moodstocks寶石(https://github.com/adelevie/moodstocks)。您將遇到另一個問題,即(AFAIK)RestClient無法處理HTTP摘要身份驗證。

你需要使用另一個庫,例如HTTParty。您仍然可以使用RestClient :: Payload :: Multipart來生成有效負載,如下所示:https://github.com/Moodstocks/moodstocks-api/blob/master/moodstocks-api/msapi.rb

如果需要,也可以使用cURL綁定或Rufus :: Verbs之一。