将CURL请求重新分解为Ruby的RestClient

时间:2011-11-11 17:01:36

标签: ruby curl put rest-client

我无法使用RestClient将此CURL请求转换为Ruby:

system("curl --digest -u #{@user}:#{@pass} '#{@endpoint}/#{id}' --form image_file=@'#{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'

3 个答案:

答案 0 :(得分:2)

在curl请求中,您通过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 :(得分:1)

Robustus是对的,你也可以使用RestClient :: Payload :: Multipart。

但是我已经看到你问这个你的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。

答案 2 :(得分:0)

您需要检查请求以确定它们的不同之处。您可以尝试使用wireshark抓取您的流量,或通过fiddlercharles代理请求。

相关问题