Skip to content

wreq.cookie

Cookie management and storage for maintaining session state.

wreq.cookie

HTTP Cookie Management

This module provides classes for creating, managing, and storing HTTP cookies in a thread-safe manner. It includes support for all standard cookie attributes and provides a cookie jar for automatic cookie handling during HTTP requests.

SameSite

Bases: Enum

The Cookie SameSite attribute.

Source code in python/wreq/cookie.py
@final
class SameSite(Enum):
    r"""
    The Cookie SameSite attribute.
    """

    Strict = auto()
    Lax = auto()
    Empty = auto()

Cookie

A cookie.

Source code in python/wreq/cookie.py
class Cookie:
    r"""
    A cookie.
    """

    name: str
    r"""
    The name of the cookie.
    """
    value: str
    r"""
    The value of the cookie.
    """
    http_only: bool
    r"""
    Returns true if the 'HttpOnly' directive is enabled.
    """
    secure: bool
    r"""
    Returns true if the 'Secure' directive is enabled.
    """
    same_site_lax: bool
    r"""
    Returns true if  'SameSite' directive is 'Lax'.
    """
    same_site_strict: bool
    r"""
    Returns true if  'SameSite' directive is 'Strict'.
    """
    path: str | None
    r"""
    Returns the path directive of the cookie, if set.
    """
    domain: str | None
    r"""
    Returns the domain directive of the cookie, if set.
    """
    max_age: datetime.timedelta | None
    r"""
    Get the Max-Age information.
    """
    expires: datetime.datetime | None
    r"""
    The cookie expiration time.
    """

    def __init__(
        self,
        name: str,
        value: str,
        domain: str | None = None,
        path: str | None = None,
        max_age: datetime.timedelta | None = None,
        expires: datetime.datetime | None = None,
        http_only: bool | None = None,
        secure: bool | None = None,
        same_site: SameSite | None = None,
    ) -> None:
        r"""
        Create a new cookie.
        """
        ...

    def __str__(self) -> str: ...

name instance-attribute

name

The name of the cookie.

value instance-attribute

value

The value of the cookie.

http_only instance-attribute

http_only

Returns true if the 'HttpOnly' directive is enabled.

secure instance-attribute

secure

Returns true if the 'Secure' directive is enabled.

same_site_lax instance-attribute

same_site_lax

Returns true if 'SameSite' directive is 'Lax'.

same_site_strict instance-attribute

same_site_strict

Returns true if 'SameSite' directive is 'Strict'.

path instance-attribute

path

Returns the path directive of the cookie, if set.

domain instance-attribute

domain

Returns the domain directive of the cookie, if set.

max_age instance-attribute

max_age

Get the Max-Age information.

expires instance-attribute

expires

The cookie expiration time.

__init__

__init__(name, value, domain=None, path=None, max_age=None, expires=None, http_only=None, secure=None, same_site=None)

Create a new cookie.

Source code in python/wreq/cookie.py
def __init__(
    self,
    name: str,
    value: str,
    domain: str | None = None,
    path: str | None = None,
    max_age: datetime.timedelta | None = None,
    expires: datetime.datetime | None = None,
    http_only: bool | None = None,
    secure: bool | None = None,
    same_site: SameSite | None = None,
) -> None:
    r"""
    Create a new cookie.
    """
    ...

Jar

A thread-safe cookie jar for storing and managing HTTP cookies.

Jar can be shared across multiple threads and tasks. When passed to a client, it is used to automatically persist and send cookies across requests and responses.

Protocol-level behaviour

  • HTTP/1.1 — all cookies are folded into a single Cookie header, as required by RFC 9112 §5.6.3.
  • HTTP/2 and above — each cookie is sent as an individual header field per RFC 9113 §8.1.2.5.
Source code in python/wreq/cookie.py
class Jar:
    r"""
    A thread-safe cookie jar for storing and managing HTTP cookies.

    `Jar` can be shared across multiple threads and tasks. When passed to a
    client, it is used to automatically persist and send cookies across
    requests and responses.

    **Protocol-level behaviour**

    - **HTTP/1.1** — all cookies are folded into a single `Cookie` header,
      as required by [RFC 9112 §5.6.3].
    - **HTTP/2 and above** — each cookie is sent as an individual header
      field per [RFC 9113 §8.1.2.5].

    [RFC 9112 §5.6.3]: https://www.rfc-editor.org/rfc/rfc9112#section-5.6.3
    [RFC 9113 §8.1.2.5]: https://www.rfc-editor.org/rfc/rfc9113#section-8.1.2.5
    """

    def __init__(self) -> None:
        r"""
        Create a new empty cookie jar.
        """
        ...

    def get(self, name: str, url: str) -> Cookie | None:
        r"""
        Look up a cookie by name scoped to the given URL.

        Domain matching is exact: only cookies whose domain exactly matches
        the URL host are considered. Subdomains are not matched.

        Returns `None` if no matching cookie is found.

        Args:
            name: The cookie name to look up.
            url: The URL used for exact domain matching and path matching.
        """
        ...

    def get_all(self) -> Sequence[Cookie]:
        r"""
        Return all cookies currently stored in the jar.
        """
        ...

    def add(self, cookie: Cookie | str, url: str) -> None:
        r"""
        Insert a cookie into the jar, scoped to the given URL.

        Args:
            cookie: A `Cookie` object or a raw `Set-Cookie` header string.
            url: The URL the cookie originates from (used for domain / path scoping).

        Example:
            ```python
            jar.add("session=abc123; Path=/; HttpOnly", "https://example.com")
            ```
        """
        ...

    def remove(self, name: str, url: str) -> None:
        r"""
        Remove a cookie by name, scoped to the given URL.

        Args:
            name: The cookie name to remove.
            url: The URL the cookie is scoped to.
        """
        ...

    def clear(self) -> None:
        r"""
        Remove all cookies from the jar.
        """
        ...

__init__

__init__()

Create a new empty cookie jar.

Source code in python/wreq/cookie.py
def __init__(self) -> None:
    r"""
    Create a new empty cookie jar.
    """
    ...

get

get(name, url)

Look up a cookie by name scoped to the given URL.

Domain matching is exact: only cookies whose domain exactly matches the URL host are considered. Subdomains are not matched.

Returns None if no matching cookie is found.

Parameters:

Name Type Description Default
name str

The cookie name to look up.

required
url str

The URL used for exact domain matching and path matching.

required
Source code in python/wreq/cookie.py
def get(self, name: str, url: str) -> Cookie | None:
    r"""
    Look up a cookie by name scoped to the given URL.

    Domain matching is exact: only cookies whose domain exactly matches
    the URL host are considered. Subdomains are not matched.

    Returns `None` if no matching cookie is found.

    Args:
        name: The cookie name to look up.
        url: The URL used for exact domain matching and path matching.
    """
    ...

get_all

get_all()

Return all cookies currently stored in the jar.

Source code in python/wreq/cookie.py
def get_all(self) -> Sequence[Cookie]:
    r"""
    Return all cookies currently stored in the jar.
    """
    ...

add

add(cookie, url)

Insert a cookie into the jar, scoped to the given URL.

Parameters:

Name Type Description Default
cookie Cookie | str

A Cookie object or a raw Set-Cookie header string.

required
url str

The URL the cookie originates from (used for domain / path scoping).

required
Example
jar.add("session=abc123; Path=/; HttpOnly", "https://example.com")
Source code in python/wreq/cookie.py
def add(self, cookie: Cookie | str, url: str) -> None:
    r"""
    Insert a cookie into the jar, scoped to the given URL.

    Args:
        cookie: A `Cookie` object or a raw `Set-Cookie` header string.
        url: The URL the cookie originates from (used for domain / path scoping).

    Example:
        ```python
        jar.add("session=abc123; Path=/; HttpOnly", "https://example.com")
        ```
    """
    ...

remove

remove(name, url)

Remove a cookie by name, scoped to the given URL.

Parameters:

Name Type Description Default
name str

The cookie name to remove.

required
url str

The URL the cookie is scoped to.

required
Source code in python/wreq/cookie.py
def remove(self, name: str, url: str) -> None:
    r"""
    Remove a cookie by name, scoped to the given URL.

    Args:
        name: The cookie name to remove.
        url: The URL the cookie is scoped to.
    """
    ...

clear

clear()

Remove all cookies from the jar.

Source code in python/wreq/cookie.py
def clear(self) -> None:
    r"""
    Remove all cookies from the jar.
    """
    ...