Skip to content

Can't put cookie jar into a request #511

Closed

Description

@danini-the-panini

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.

Activity

  1. tarcieri commented on Nov 29, 2018

    @tarcieri
    Member

    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.

  2. odinhb commented on Sep 25, 2020

    @odinhb
    Contributor

    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)
  3. HoneyryderChuck commented on Sep 25, 2020

    @HoneyryderChuck
  4. tarcieri commented on Sep 25, 2020

    @tarcieri
    Member

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

  5. sferik commented on Mar 12, 2026

    @sferik
    Contributor

    Chainable#cookies now accepts any enumerable of HTTP::Cookie objects in addition to a Hash, so passing a cookie jar from a previous response works directly:

    response = HTTP.get("http://example.com")
    HTTP.cookies(response.cookies).get("http://example.com/next")

    This was part of a broader cookie API refactor (0dd249f) that removed cookie management from Options and simplified Chainable#cookies to set the Cookie header directly from a Hash or Array<HTTP::Cookie>. Since HTTP::CookieJar is Enumerable and yields HTTP::Cookie objects, it should work transparently.

    Additionally, cookie jar management during redirects is now handled automatically by the Session (008c181), so manual cookie passing between redirect steps is no longer necessary.

    If you encounter any issues with the new cookie API, please reopen this issue or file a new one.

  6. added theissue type on Mar 17, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions