From f5c03878b1386bfc7882b8af6fe97d22655a7937 Mon Sep 17 00:00:00 2001 From: Amrita kumari mishra Date: Sun, 23 Nov 2025 19:19:15 +0000 Subject: [PATCH 1/2] Fix: Ensure empty lists in params are encoded as empty keys #6557 --- src/requests/models.py | 9 ++++++++- tests/test_requests.py | 15 +++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/src/requests/models.py b/src/requests/models.py index c4b25fa079..4e38f86ae5 100644 --- a/src/requests/models.py +++ b/src/requests/models.py @@ -119,8 +119,15 @@ def _encode_params(data): elif hasattr(data, "__iter__"): result = [] for k, vs in to_key_val_list(data): - if isinstance(vs, basestring) or not hasattr(vs, "__iter__"): + if isinstance(vs, (str, bytes)) or not hasattr(vs, '__iter__'): vs = [vs] + + # --- START FIX --- + # If vs is an empty list [], treat it as [''] so it yields one empty key + if not vs: + vs = [''] + # --- END FIX --- + for v in vs: if v is not None: result.append( diff --git a/tests/test_requests.py b/tests/test_requests.py index 75d2deff2e..71a589bb14 100644 --- a/tests/test_requests.py +++ b/tests/test_requests.py @@ -119,6 +119,21 @@ def test_basic_building(self): assert pr.url == req.url assert pr.body == "life=42" + def test_params_with_empty_list_are_included(self): + """ + Regression test for #6557: Ensure empty lists in params + result in the key being present in the URL (e.g. key=). + """ + url = 'http://example.com' + params = {'key': []} + req = requests.Request('GET', url, params=params) + prep = req.prepare() + + # Check if the key is present with an empty value + assert 'key=' in prep.url + # Check that it didn't literally print the brackets + assert 'key=[]' not in prep.url + @pytest.mark.parametrize("method", ("GET", "HEAD")) def test_no_content_length(self, httpbin, method): req = requests.Request(method, httpbin(method.lower())).prepare() From 73b297202938f6b2286535d557271f7b25d1d552 Mon Sep 17 00:00:00 2001 From: Amrita kumari mishra Date: Wed, 10 Dec 2025 11:40:50 +0000 Subject: [PATCH 2/2] Add simplejson as an optional extra --- setup.py | 1 + 1 file changed, 1 insertion(+) diff --git a/setup.py b/setup.py index ff65d39102..39ab804e3e 100755 --- a/setup.py +++ b/setup.py @@ -99,6 +99,7 @@ "security": [], "socks": ["PySocks>=1.5.6, !=1.5.7"], "use_chardet_on_py3": ["chardet>=3.0.2,<6"], + "simplejson": ["simplejson"], }, project_urls={ "Documentation": "https://requests.readthedocs.io",