将 CURL 请求重构为 Ruby 的 RestClient

发布于 12-14 17:14 字数 635 浏览 5 评论 0原文

我在使用 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'

I'm having trouble translating this CURL request into Ruby using RestClient:

system("curl --digest -u #{@user}:#{@pass} '#{@endpoint}/#{id}' --form image_file=@'#{path}' -X PUT")

I keep getting 400 Bad Request errors. As far as I can tell, the request does get properly authenticated, but hangs up from the file upload part. Here are my best attempts, all of which get me those 400 errors:

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'

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

坐在坟头思考人生2024-12-21 17:14:36

您需要检查请求以确定它们的不同之处。您可以尝试使用 wireshark 捕获流量或通过 wireshark 代理请求。 fiddler2.com/fiddler2/" rel="nofollow">fiddler 或 查尔斯

You need to examine the requests to determine where they differ. You can try capturing your traffic with wireshark or proxying the requests through fiddler or charles.

笑咖2024-12-21 17:14:34

在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

in the curl request you are sending multipart form-data via the PUT-request, so accordingly, you need to do the same in RestClient:

resource = RestClient::Resource.new "#{@endpoint}/#{id}", @user, @pass
resource.put :image_file => File.new(path, 'rb'), :content_type => 'multipart/form-data', :multipart => true
层林尽染2024-12-21 17:14:34

Robustus 是对的,您还可以使用 RestClient::Payload::Multipart。

不过,我发现您正在询问您的 Moodstocks gem (https://github.com/adelevie/moodstocks)。您还会遇到另一个问题,即(据我所知)RestClient 无法处理 HTTP Digest 身份验证。

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

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

Robustus is right, you could also use a RestClient::Payload::Multipart.

However I have seen that you're asking this for your Moodstocks gem (https://github.com/adelevie/moodstocks). You will have another problem which is that (AFAIK) RestClient cannot handle HTTP Digest authentication.

You will need to use another library such as HTTParty for that. You can still use RestClient::Payload::Multipart to generate the payload as demonstrated here: https://github.com/Moodstocks/moodstocks-api/blob/master/moodstocks-api/msapi.rb

You could also use one of the cURL bindings or Rufus::Verbs if you want.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文