我尝试上传文件时,Rest客户端(Ruby)不设置cookie

时间:2014-04-26 05:23:43

标签: ruby http rest cookies rest-client

require 'rubygems'
require 'rest_client'
response = RestClient.post "URL",
                              "myfile" => File.new("/path/to/file"),
                              "Cookie" => "Name=michal",
                              "X-SESSION-ID" => "dsafasdfadsfadsfasfdadf",
                              "User-Agent" => "UNknow",
                              "connection" => "Keep-Alive"

如果我尝试使用上述代码发布文件,那么headers Cookie,用户代理,X-SESSION-ID 永远不会被设置在发出请求...我使用wireshark

确认

我做错了什么?

1 个答案:

答案 0 :(得分:2)

RestClient在检测到文件时会尝试智能,并将其余参数视为多部分请求中的其他部分。因此,您只需将内容与标题分开即可将其更改为哈希值。

试试这个:

response = RestClient.post "URL",
                           {"myfile" => File.new("/path/to/file")},
                           "Cookie" => "Name=michal",
                           "X-SESSION-ID" => "dsafasdfadsfadsfasfdadf",
                           "User-Agent" => "UNknow",
                           "connection" => "Keep-Alive"