如何在Ruby中使用RestClient上传文件?

时间:2014-11-25 01:40:17

标签: ruby rest-client

我正在尝试在Ruby上传文件。下面是使用curl的REST调用示例。

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.

有些人可以在Ruby中给我一个相当于上面的内容。 我尝试过以下方法,但没有成功。

$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
)

提前感谢任何指导。

拉​​维..

1 个答案:

答案 0 :(得分:1)

最后得到了代码。清除了臭臭的代码。 以下是在发布多部分/混合模式数据时运行良好的语法。

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