Robin Robin - 6 months ago 62
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 = => :get, :url => @base_url + path, :user => @sid, :password => @token).execute

and how to post a file as multipart form data '/data', :myfile =>"/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 =
          :method => :post,
          :url => '/data',
          :user => @sid,
          :password => @token,
          :payload => {
            :multipart => true,
            :file =>"/path/to/image.jpg", 'rb')
response = request.execute
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download