我無法翻譯使用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'
您不應將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 –
刪除content_type不會改變任何內容。我認爲這可能與提供的路徑有關。給出的路徑是一個在CURL中正常工作的絕對路徑。我怎樣才能讓File.read處理絕對路徑? – user94154