3

我无法使用 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'
4

3 回答 3

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
于 2011-11-19T14:48:25.923 回答
1

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

但是,我看到您正在为您的 Moodstocks gem (https://github.com/adelevie/moodstocks) 询问这个。您将遇到另一个问题,即 (AFAIK) RestClient 无法处理 HTTP Digest 身份验证。

为此,您将需要使用另一个库,例如 HTTParty。您仍然可以使用 RestClient::Payload::Multipart 来生成有效负载,如下所示:https ://github.com/Moodstocks/moodstocks-api/blob/master/moodstocks-api/msapi.rb

如果需要,您还可以使用其中一种 cURL 绑定或 Rufus::Verbs。

于 2011-11-23T09:47:53.707 回答
0

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

于 2011-11-17T21:22:29.267 回答