Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 983
Robin Avatar asked Jul 09 '12 01:07

Robin


People also ask

Does REST API support multipart form data data format?

Multipart/Form-Data is a popular format for REST APIs, since it can represent each key-value pair as a “part” with its own content type and disposition. Each part is separated by a specific boundary string, and we don't explicitly need Percent Encoding for their values.

How do I upload a multipart form data?

Multipart form data: The ENCTYPE attribute of <form> tag specifies the method of encoding for the form data. It is one of the two ways of encoding the HTML form. It is specifically used when file uploading is required in HTML form. It sends the form data to server in multiple parts because of large size of file.

How does multipart file upload work?

Multipart upload allows you to upload a single object as a set of parts. Each part is a contiguous portion of the object's data. You can upload these object parts independently and in any order. If transmission of any part fails, you can retransmit that part without affecting other parts.


1 Answers

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
like image 95
robustus Avatar answered Sep 19 '22 14:09

robustus