httprb / http

HTTP (The Gem! a.k.a. http.rb) - a fast Ruby HTTP client with a chainable API, streaming support, and timeouts
MIT License
3k stars 321 forks source link

Can't put cookie jar into a request #511

Open danini-the-panini opened 5 years ago

danini-the-panini commented 5 years ago

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.

tarcieri commented 5 years ago

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.

odinhb commented 3 years ago

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)
HoneyryderChuck commented 3 years ago

@odinhb there's at least 1 http ruby library supporting it

tarcieri commented 3 years ago

We just landed #613 which adds support for it, but it isn't released