Can't put cookie jar into a request
I tried doing something like this to use the cookies from a previous response in a subsequent request:
response = HTTP.get('http://www.example.com')
cookies = response.cookies
new_response = HTTP.get('http://www.example.com').cookies(cookies)
However, this does not work as expected because response.cookies is a HTTP::CookieJar and the chainable cookies method wants a Hash.
How do I do what I'm trying to achieve here? The documentation is sparse on this point.
Reusing a HTTP::CookieJar (automatically) between requests was something I had originally suggested in the form of an HTTP::Session type: #306
It looks like you're right though, HTTP::Options#cookies does not accept an HTTP::CookieJar as an argument, and probably should.
I ran into this when replacing some cURL-ing that had been done with the justification that it was easier to use cURL to maintain the session than using http.rb or other ruby http libraries. Here's the function I used:
def cookiejar_to_hash(jar)
hash = {}
jar.cookies.each do |cookie|
hash[cookie.name] = cookie.value
end
hash
end
Allowing you to
response = HTTP.get('http://www.example.com')
cookies = cookiejar_to_hash(response.cookies)
new_response = HTTP.get('http://www.example.com').cookies(cookies)
@odinhb there's at least 1 http ruby library supporting it
We just landed #613 which adds support for it, but it isn't released