{"id":"2db9511a196b24c2","repo":"psf/requests","slug":"name-name-r-domain-domain-r-path-path-r","errorCode":null,"errorMessage":"name={name!r}, domain={domain!r}, path={path!r}","messagePattern":"name=(.+?), domain=(.+?), path=(.+?)","errorType":"exception","errorClass":"KeyError","httpStatus":null,"severity":"error","filePath":"src/requests/cookies.py","lineNumber":421,"sourceCode":"    ) -> str | None:\n        \"\"\"Requests uses this method internally to get cookie values.\n\n        If there are conflicting cookies, _find arbitrarily chooses one.\n        See _find_no_duplicates if you want an exception thrown if there are\n        conflicting cookies.\n\n        :param name: a string containing name of cookie\n        :param domain: (optional) string containing domain of cookie\n        :param path: (optional) string containing path of cookie\n        :return: cookie.value\n        \"\"\"\n        for cookie in iter(self):\n            if cookie.name == name:\n                if domain is None or cookie.domain == domain:\n                    if path is None or cookie.path == path:\n                        return cookie.value\n\n        raise KeyError(f\"name={name!r}, domain={domain!r}, path={path!r}\")\n\n    def _find_no_duplicates(\n        self, name: str, domain: str | None = None, path: str | None = None\n    ) -> str:\n        \"\"\"Both ``__get_item__`` and ``get`` call this function: it's never\n        used elsewhere in Requests.\n\n        :param name: a string containing name of cookie\n        :param domain: (optional) string containing domain of cookie\n        :param path: (optional) string containing path of cookie\n        :raises KeyError: if cookie is not found\n        :raises CookieConflictError: if there are multiple cookies\n            that match name and optionally domain and path\n        :return: cookie.value\n        \"\"\"\n        toReturn = None\n        for cookie in iter(self):\n            if cookie.name == name:","sourceCodeStart":403,"sourceCodeEnd":439,"githubUrl":"https://github.com/psf/requests/blob/414f0513c33883adf6f2b46901d4f0b38a455851/src/requests/cookies.py#L403-L439","documentation":"A KeyError raised by RequestsCookieJar._find() in src/requests/cookies.py when no cookie in the jar matches the requested name (and optional domain/path filters). _find is the internal lookup Requests uses to read cookie values; the error message echoes the search criteria (name, domain, path) so you can see exactly what was asked for.","triggerScenarios":"Iterating jar lookups after a request: session.cookies['csrftoken'] style access paths that route through _find, or calling internal helpers with a name the server never set. With domain/path arguments supplied, a cookie with the right name but different domain/path also raises this.","commonSituations":"Expecting a cookie the server only sets after login or on a different domain/subdomain (e.g. '.example.com' vs 'api.example.com'); Secure cookies dropped because the request was plain HTTP; redirects that consumed the Set-Cookie on an intermediate response; typo'd cookie names.","solutions":["Use jar.get('name', default) instead of indexing to avoid the exception when absence is expected.","Print list(session.cookies) or session.cookies.list_domains() to see what was actually stored, and match the domain/path filters to it.","Ensure the request that sets the cookie actually ran first (login/handshake), and that you're using a Session so cookies persist across requests.","Use HTTPS if the cookie is marked Secure."],"exampleFix":"# before\ntoken = session.cookies['csrftoken']\n\n# after\ntoken = session.cookies.get('csrftoken')\nif token is None:\n    raise RuntimeError(f\"csrftoken not set; jar has: {[c.name for c in session.cookies]}\")","handlingStrategy":"try-catch","validationCode":"# Check existence before removing/accessing a specific cookie\nexists = any(c.name == name and (domain is None or c.domain == domain) and (path is None or c.path == path) for c in jar)","typeGuard":"def cookie_exists(jar, name, domain=None, path=None):\n    return any(c.name == name and (domain is None or c.domain == domain) and (path is None or c.path == path) for c in jar)","tryCatchPattern":"try:\n    jar.clear(domain, path, name)\nexcept KeyError:\n    pass  # cookie not present","preventionTips":["Catch KeyError when calling jar.clear() or deleting a cookie that may not exist","Iterate the jar to confirm exact domain/path values first — cookie lookup requires exact matches, and domains often carry a leading dot ('.example.com')","Prefer jar.get(name, default=None) over jar[name] when absence is expected"],"tags":["cookies","keyerror","session","lookup"],"analyzedSha":"414f0513c33883adf6f2b46901d4f0b38a455851","analyzedAt":"2026-07-31T19:24:57.696Z","schemaVersion":2}