ruby - 重构对 Ruby 的 RestClient 的 CURL 请求

标签 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'

最佳答案

在 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/

相关文章:

json - cURL GET 查询字符串是一个 JSON

java - PUT:PathVariable 和 RequestParam 不能一起工作

ruby-on-rails - 显示所有有工作的公司

与 Gosu 的 Ruby 生命游戏 - 更新操作不起作用

http - Fail2Ban 通过 cURL 进行 POST

c++ - 使用 curl 下载文件时确定扩展名?

laravel - 什么时候在 Laravel 中使用 PATCH 和 PUT 方法?

asp.net - 找不到 Http 404

c# - Rails 和 ASP.NET 单点登录

ruby - Cron 作业无法加载 gem