将 CURL 请求重构为 Ruby 的 RestClient
我在使用 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技术交流群](/public/img/jiaqun_03.jpg)
发布评论
评论(3)
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。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
您需要检查请求以确定它们的不同之处。您可以尝试使用 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.