Search code examples
rubyhttprestcookiesrest-client

Rest client (Ruby) does not set the cookie when i try to upload a file


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"

If I try to use the above code to post a file then the headers Cookie,User-Agent,X-SESSION-ID never gets set on the request that is send out... i confirmed it using wireshark

What am i doing wrong ?


Solution

  • RestClient tries to be smart when it detects a file and treat the remaining arguments as other parts in the multipart request. So you just need to separate the content from the headers by change it into a hash.

    Try this:

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