Вопрос

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 ?

Это было полезно?

Решение

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"
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top