The :cookies plugin allows for cookies to be stored, managed and used in the context of a session:

http = HTTPX.plugin(:cookies)
response = http.get("") #=> "302 ... Location: ... Set-Cookie: aadm...."
response = http.get(response.headers["location"]) #=> Sends the cookie information

This plugin also works seamlessly with the :follow_redirects plugin, in that redirect responses setting a session cookie will be forwarded:

http = HTTPX.plugin(:cookies).plugin(:follow_redirects)
response = http.get("") #=> will redirect to "" with cookies


A user can also initiate the cookie jar when starting the session as well.

You can do so using the :cookies option. This can be:

  1. pairs of cookie name and values (and extra options);
  2. an array of options (where :name (and :value) is defined);
  3. a cookie jar from another session;
# 1.
http = HTTPX.plugin(:cookies).with_cookies("foo" => "bar")
# 2.
http = HTTPX.plugin(:cookies).with_cookies([{ name: "foo", value: "bar" }])
# 3.
http = HTTPX.plugin(:cookies)
response = http.get("")
# ... after a while
other_http = HTTPX.plugin(:cookies)

The extra options when defining a cookie correspond to the supported cookie attributes as per RFC6265:

  • :secure: The “Secure” attribute, can be true or false (false by default);
  • :path: The “Path” attribute ("/" by default);
  • :domain: The “Domain” attribute (when not set, it’ll be sent in all session requests);
  • :max_age: The “Max-Age” attribute, can an integer (seconds) setting a lifetime for the cookie (when cookie expires, it’ll be dropped from the jar);
  • :expires: The “Expires” attribute, can be a Time object, setting the moment after which the cookie is expired and is dropped from the jar);

Next: Server Push