Skip to content

Commit 37f376a

Browse files
committed
Fix content_type in get request when data has only None values
1 parent 2d55176 commit 37f376a

File tree

2 files changed

+28
-2
lines changed

2 files changed

+28
-2
lines changed

requests/models.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -507,8 +507,8 @@ def prepare_body(self, data, files, json=None):
507507
(body, content_type) = self._encode_files(files, data)
508508
else:
509509
if data:
510-
body = self._encode_params(data)
511-
if isinstance(data, basestring) or hasattr(data, 'read'):
510+
body = self._encode_params(data) or None
511+
if isinstance(data, basestring) or hasattr(data, "read"):
512512
content_type = None
513513
else:
514514
content_type = 'application/x-www-form-urlencoded'

tests/test_lowlevel.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -427,3 +427,29 @@ def response_handler(sock):
427427
assert isinstance(excinfo.value, requests.exceptions.RequestException)
428428
assert isinstance(excinfo.value, JSONDecodeError)
429429
assert r.text not in str(excinfo.value)
430+
431+
432+
@pytest.mark.parametrize(
433+
"method,include,exclude",
434+
(
435+
(requests.get, [], [b"Content-Length:", b"Transfer-Encoding:"]),
436+
(requests.post, [b"Content-Length: 0\r\n"], [b"Transfer-Encoding:"]),
437+
)
438+
)
439+
def test_empty_urlencoded_form_body(method, include, exclude):
440+
"""Ensure we use only the specified Host header for chunked requests."""
441+
close_server = threading.Event()
442+
server = Server(echo_response_handler, wait_to_close_event=close_server)
443+
444+
with server as (host, port):
445+
url = f"http://{host}:{port}/"
446+
resp = method(url, data=(("a", None,),))
447+
close_server.set() # release server block
448+
449+
assert not resp.content.endswith(b"\r\n0\r\n\r\n")
450+
451+
for header in include:
452+
assert header in resp.content
453+
454+
for header in exclude:
455+
assert header not in resp.content

0 commit comments

Comments
 (0)