Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion src/requests/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
15 changes: 15 additions & 0 deletions tests/test_requests.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down