Ruby rest-client文件作为具有基本身份validation的多部分表单数据上载
我理解如何使用Ruby的rest-client使用基本身份validation来创建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')
但我似乎无法弄清楚如何将两者结合起来将文件发布到需要基本身份validation的服务器。 有谁知道创建此请求的最佳方法是什么?
如何使用RestClient::Payload
与RestClient::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
最新的最佳方式可能是: 链接在此处输入链接描述
RestClient.post( url, { :transfer => { :path => '/foo/bar', :owner => 'that_guy', :group => 'those_guys' }, :upload => { :file => File.new(path, 'rb') } })
这是一个文件和一些json数据的示例:
require 'rest-client' payload = { :multipart => true, :file => File.new('/path/to/file', 'rb'), :data => {foo: {bar: true}}.to_json } r = RestClient.post(url, payload, :authorization => token)
RestClient API似乎已经改变。 以下是使用基本身份validation上传文件的最新方法:
response = RestClient::Request.execute( method: :post, url: url, user: 'username', password: 'password', timeout: 600, # Optional payload: { multipart: true, file: File.new('/path/to/file, 'rb') } )