# Add a CookieJar.get\_cookie method

**URL:** <https://discuss.python.org/t/add-a-cookiejar-get-cookie-method/26368>\
**Category:** Ideas\
**Created:** [May 1, 2023, 7:24am UTC](https://discuss.python.org/t/add-a-cookiejar-get-cookie-method/26368 "2023-05-01T07:24:59Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![alexei](https://avatars.discourse-cdn.com/v4/letter/a/c77e96/32.png) [@alexei](https://discuss.python.org/u/alexei)\
**Post date:** [May 1, 2023, 7:24am UTC](https://discuss.python.org/t/add-a-cookiejar-get-cookie-method/26368/1 "2023-05-01T07:24:59Z")

</div>

The cookie jar offers methods for writing (`set_cookie`, `clear`), but there’s no high level way to reading any of it i.e. no `get_cookie`.

Given a cookie jar `jar`, if one wanted to read the cookies, they’d need to iterate over it:

```python
for cookie in jar:
    ...

```

Getting a specific cookie only adds to that (assuming a cookie set with `domain`, `path`, `name`):

```python
for cookie in jar:
    if cookie.name == name:
        if cookie.domain == domain:
            if cookie.path == path:
                ...

```

An alternative is to access the underlying dictionary:

```python
jar._cookies[domain][path][name]

```

In my particular case, I need to check cookies in unit tests (that e.g. they’re set correctly etc). While I can (and do) read from `._cookies`, I’m always wary of accessing `_` attributes - it feels like I’m doing something I shouldn’t. A very simple implementation would just officialize it:

```python
    def get_cookie(domain: str, path: str, name: str) -> Cookie:
        return self._cookies[domain][path][name]

```

Things to consider:

- what to do when the desired cookie doesn’t exist; e.g. `KeyError` works for me;
- whether `domain` and `path` should be optional;
- …which could then lead to a situation when there are multiple cookies with the same name (but different domains, paths).

If this is of interest, I can open a PR etc.

GH issue: [Add a `CookieJar.get_cookie` method · Issue #104019 · python/cpython · GitHub](https://github.com/python/cpython/issues/104019)

---

<div class="post-metadata">

**Author:** ![kknechtel](https://avatars.discourse-cdn.com/v4/letter/k/e47c2d/32.png) [@kknechtel](https://discuss.python.org/u/kknechtel)\
**Post date:** [May 1, 2023, 11:54pm UTC](https://discuss.python.org/t/add-a-cookiejar-get-cookie-method/26368/2 "2023-05-01T23:54:39Z")

</div>

> [@alexei](#):
>
> A very simple implementation would just officialize it:

I’m unfamiliar with the library in question, but I think I understand what you’re talking about well enough to comment. What you describe sounds good.

> [@alexei](#):
>
> what to do when the desired cookie doesn’t exist; e.g. `KeyError` works for me;

Agreed; I can’t see a reason to complicate this.

> [@alexei](#):
>
> - whether `domain` and `path` should be optional;
> - …which could then lead to a situation when there are multiple cookies with the same name (but different domains, paths).

They should definitely not be optional. Trying to do this with only a cookie name provided is a “searching” task, not simply direct access of a value. It should use a separate function, and then it can easily just return a list of everything that matches. It might have some more bells and whistles, too; that’s where the actual design work starts IMO.
