Robin Robin - 1 month ago 8
HTTP Question

Ruby rest-client file upload as multipart form data with basic authenticaion

I understand how to make an http request using basic authentication with Ruby's rest-client

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


and how to post a file as multipart form data

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


but I can't seem to figure out how to combine the two in order to post a file to a server which requires basic authentication. Does anyone know what is the best way to create this request?

Answer Source

How about using a RestClient::Payload with RestClient::Request... For an example:

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