Ruby rest-client 文件上传为具有基本身份验证的多部分表单数据

标签 ruby http post basic-authentication rest-client

我了解如何通过 Ruby 的 rest-client 使用基本身份验证发出 http 请求

response = RestClient::Request.new(:method => :get, :url => @base_url + path, :user => @sid, :password => @token).execute

以及如何将文件作为多部分表单数据发布

RestClient.post '/data', :myfile => File.new("/path/to/image.jpg", 'rb')

但我似乎无法弄清楚如何将两者结合起来以便将文件发布到需要基本身份验证的服务器。有谁知道创建此请求的最佳方式是什么?

最佳答案

如何使用 RestClient::PayloadRestClient::Request... 例如:

request = RestClient::Request.new(
          :method => :post,
          :url => '/data',
          :user => @sid,
          :password => @token,
          :payload => {
            :multipart => true,
            :file => File.new("/path/to/image.jpg", 'rb')
          })      
response = request.execute

关于Ruby rest-client 文件上传为具有基本身份验证的多部分表单数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11388090/

相关文章:

ruby-on-rails - 如何设置所有 Rails 事务的隔离级别

javascript - 从 PHP 获取实时回复

ruby-on-rails - 如何在邮寄方法的邮寄布局中包含条件?

Ruby 打印散列键和值

ruby-on-rails - 如何在 rspec 中跳过 x 次测试

json - 如何在 iOS5 中将 JSON 数据对象发布到服务器?

javascript - 无法使用 JavaScript 发送 POST-JSON-Request

java - OkHttpClient 将 http URL 重定向到 Https 并给出带有 https URL 的 SSLException

python - urllib.Request 删除 Content-Type header

c# - 通过 Selenium WebDriver C# 获取 HTTP 状态码