http icon indicating copy to clipboard operation
http copied to clipboard

Can't put cookie jar into a request

Open danini-the-panini opened this issue 7 years ago • 4 comments

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.

danini-the-panini avatar Nov 29 '18 10:11 danini-the-panini

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.

tarcieri avatar Nov 29 '18 15:11 tarcieri

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 avatar Sep 25 '20 09:09 odinhb

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

HoneyryderChuck avatar Sep 25 '20 12:09 HoneyryderChuck

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

tarcieri avatar Sep 25 '20 14:09 tarcieri