我在使用 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'
最佳答案
在 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
关于ruby - 重构对 Ruby 的 RestClient 的 CURL 请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8097373/