From a3d9a050e7c0e13d1b0ffa4e033bf2016a1f04bf Mon Sep 17 00:00:00 2001 From: shindonghwi Date: Fri, 19 Dec 2025 12:24:35 +0900 Subject: [PATCH] fix: handle empty string cookie values correctly _find_no_duplicates() was using 'if toReturn:' which treats empty string as falsy, causing KeyError when retrieving cookies with empty string values. Changed to 'if toReturn is not None:' to properly handle this case. --- src/requests/cookies.py | 2 +- tests/test_requests.py | 8 ++++++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/src/requests/cookies.py b/src/requests/cookies.py index f69d0cda9e..fc1fec6bf5 100644 --- a/src/requests/cookies.py +++ b/src/requests/cookies.py @@ -408,7 +408,7 @@ def _find_no_duplicates(self, name, domain=None, path=None): # we will eventually return this as long as no cookie conflict toReturn = cookie.value - if toReturn: + if toReturn is not None: return toReturn raise KeyError(f"name={name!r}, domain={domain!r}, path={path!r}") diff --git a/tests/test_requests.py b/tests/test_requests.py index 75d2deff2e..e0100b5401 100644 --- a/tests/test_requests.py +++ b/tests/test_requests.py @@ -1299,6 +1299,14 @@ def test_cookie_as_dict_keeps_len(self): assert len(d2) == 2 assert len(d3) == 2 + def test_cookie_empty_value(self): + """Empty string cookie values should be retrievable.""" + jar = requests.cookies.RequestsCookieJar() + jar.set("empty", "") + + assert jar["empty"] == "" + assert jar.get("empty") == "" + def test_cookie_as_dict_keeps_items(self): key = "some_cookie" value = "some_value"