How to upload a file using RestClient in Ruby?

为君一笑 提交于 2019-12-11 11:41:42

问题


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 "content=@MyFile.jpg; 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:


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, }
)


来源:https://stackoverflow.com/questions/27117408/how-to-upload-a-file-using-restclient-in-ruby

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!