Here is the code that I can run successfully.
First of all, your cookie is an array, and in my code it should be an array with a replacement (or hash). When I run the code in a real application, the GET_URL response returns a session cookie, but the POST_URL response returns a different session cookie.
Adjust the parsing so that you get each name and cookie value:
def merge_cookies_into_cookie_jar(response) x = response.headers_hash['set-cookie'] case x ... when String x.split('; ').each{|cookie| key,value=cookie.split('=', 2) @cookie_jar[key]=value } end end
The cookie box needs to be converted to a string:
def cookie_jar_to_s @cookie_jar.to_a.map{|key, val| "#{key}=#{val}"}.join("; ") end
Finally, change the headers to use the new cookie_jar_to_s:
:headers => { 'Cookie' => cookie_jar_to_s }
The bonus will be to make the cookie jar your class, maybe something like this:
class CookieJar < Hash def to_s self.to_a.map{|key, val| "#{key}=#{val}"}.join("; ") end def parse(*cookie_strings) cookie_strings.each{|s| s.split('; ').each{|cookie| key,value=cookie.split('=', 2) self.[key]=value } } end end
source share