How to upload a file using RestClient in Ruby?

3k Views Asked by At

I am trying to upload a file in Ruby. Below is a sample of the REST call using curl.

curl --sslv3 -k -v -i -X POST \
-H "Content-Type: multipart/mixed" \
-H "Accept: application/json" \
-H "Cookie: token=ABC; SESSION=13217EBA128F6ABC9E9AD095D602E4AB;" \
-F "metadata=@input_payload_file.json; type=application/json" \
-F "[email protected]; type=image/jpg"  \
https://abc.com/attachments.

Can some please give me an equivalent of above in Ruby. I have tried the following, with no success.

$fileitem = File.new('C:\log.txt', 'rb');
$fileinfo = '{"fileName":"log.txt", "resourceName":"log.txt",  "description":"Created using REST"}';

response =RestClient::Request.execute(
   :method => :post,
   :url => $UploadURL,
   :headers => {:content_type => 'multipart/mixed', :accept => 'application/json'},
   :cookies => {:token=> :"#{cust}",:SESSIONID => :"#{ssid}"},
   :payload => $fileinfo,
   :myfile => $fileitem
)

Thanks in advance for any guidance.

Ravi..

1

There are 1 best solutions below

0
On

Finally got the code working. Cleared up the smelly code. Below is the syntax that worked well while posting a multipart/mixed mode data.

response =RestClient::Request.execute(
   :method => :post,
   :url => uploadURL,
   :headers => {:content_type => 'multipart/mixed', :accept => 'application/json'},
   :cookies => {:token => cust,:SESSIONID => ssid },
   :payload => {:metadata => fileinfo, :content => fileitem, }
)