Skip to content
Merged
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
11 changes: 11 additions & 0 deletions src/ahttpx/_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,17 @@ def __init__(
elif content_length > 0:
self.headers = self.headers.copy_set("Content-Length", str(content_length))

elif method in ("POST", "PUT", "PATCH"):
# https://datatracker.ietf.org/doc/html/rfc7230#section-3.3.2
# RFC 7230, Section 3.3.2, Content Length.
#
# A user agent SHOULD send a Content-Length in a request message when no
# Transfer-Encoding is sent and the request method defines a meaning for
# an enclosed payload body. For example, a Content-Length header field is
# normally sent in a POST request even when the value is 0.
# (indicating an empty payload body).
self.headers = self.headers.copy_set("Content-Length", "0")

@property
def body(self) -> bytes:
if not hasattr(self, '_body'):
Expand Down
11 changes: 11 additions & 0 deletions src/httpx/_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,17 @@ def __init__(
elif content_length > 0:
self.headers = self.headers.copy_set("Content-Length", str(content_length))

elif method in ("POST", "PUT", "PATCH"):
# https://datatracker.ietf.org/doc/html/rfc7230#section-3.3.2
# RFC 7230, Section 3.3.2, Content Length.
#
# A user agent SHOULD send a Content-Length in a request message when no
# Transfer-Encoding is sent and the request method defines a meaning for
# an enclosed payload body. For example, a Content-Length header field is
# normally sent in a POST request even when the value is 0.
# (indicating an empty payload body).
self.headers = self.headers.copy_set("Content-Length", "0")

@property
def body(self) -> bytes:
if not hasattr(self, '_body'):
Expand Down
13 changes: 13 additions & 0 deletions tests/test_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,16 @@ def test_request_json():
"Content-Type": "application/json",
}
assert r.read() == b'{"msg":"Hello, world"}'


def test_request_empty_post():
r = httpx.Request("POST", "https://example.com")

assert repr(r) == "<Request [POST 'https://example.com']>"
assert r.method == "POST"
assert r.url == "https://example.com"
assert r.headers == {
"Host": "example.com",
"Content-Length": "0",
}
assert r.read() == b''