我理解如何使用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')
但我似乎无法弄清楚如何将两者合并以便将文件发布到需要基本身份验证的服务器。有谁知道创建此请求的最佳方式是什么?
答案 0 :(得分:23)
如何将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
答案 1 :(得分:1)
以下是文件和一些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)
答案 2 :(得分:1)
RestClient API似乎已经改变。这是使用基本身份验证上传文件的最新方式:
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')
}
)
答案 3 :(得分:0)
最新的最佳方式可能是: 链接是enter link description here
RestClient.post( url,
{
:transfer => {
:path => '/foo/bar',
:owner => 'that_guy',
:group => 'those_guys'
},
:upload => {
:file => File.new(path, 'rb')
}
})