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
2 changes: 1 addition & 1 deletion src/requests/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -557,7 +557,7 @@ def prepare_body(self, data, files, json=None):
(body, content_type) = self._encode_files(files, data)
else:
if data:
body = self._encode_params(data)
body = self._encode_params(data) or None
if isinstance(data, basestring) or hasattr(data, "read"):
content_type = None
else:
Expand Down
30 changes: 30 additions & 0 deletions tests/test_lowlevel.py
Original file line number Diff line number Diff line change
Expand Up @@ -426,3 +426,33 @@ def response_handler(sock):
assert isinstance(excinfo.value, requests.exceptions.RequestException)
assert isinstance(excinfo.value, JSONDecodeError)
assert r.text not in str(excinfo.value)


@pytest.mark.parametrize(
"method,include,exclude",
(
(requests.get, [], [b"Content-Length:", b"Transfer-Encoding:"]),
(requests.post, [b"Content-Length: 0\r\n"], [b"Transfer-Encoding:"]),
),
)
def test_empty_urlencoded_form_body(method, include, exclude):
"""Ensure requests with data that encodes to an empty body do not
send a spurious chunked transfer terminator (0\\r\\n\\r\\n).

See https://github.com/psf/requests/pull/6122
"""
close_server = threading.Event()
server = Server(echo_response_handler, wait_to_close_event=close_server)

with server as (host, port):
url = f"http://{host}:{port}/"
resp = method(url, data=(("a", None),))
close_server.set() # release server block

assert not resp.content.endswith(b"\r\n0\r\n\r\n")

for header in include:
assert header in resp.content

for header in exclude:
assert header not in resp.content