Skip to content
Myr

Add "Content-Length: 0" on POST, PUT, PATCH if required. (#995)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

* Add Content-Length: 0 on POST, PUT, PATCH if required. * Update tests/client/test_client.py Co-authored-by: Florimond Manca <florimond.manca@gmail.com>

authored by Tom Christie · committed by GitHub · parent 66a4537 · Browse files · 2 files+21 added−0 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Trailers

  • Co-authored-by: Florimond Manca <florimond.manca@gmail.com>

Diff stats

2 files+21 added−0 removed

  • tests:+16 added−0 removed
  • other:+5 added−0 removed

2 files+21 added−0 removed

modified:httpx/_models.py+5 added
614 unmodified lines
615616 auto_headers: typing.List[typing.Tuple[bytes, bytes]] = []617618 has_host = "host" in self.headers619 has_content_length = (620 "content-length" in self.headers or "transfer-encoding" in self.headers621 )622 has_user_agent = "user-agent" in self.headers623 has_accept = "accept" in self.headers624 has_accept_encoding = "accept-encoding" in self.headers625 has_connection = "connection" in self.headers
2 unmodified lines
628 url = self.url629 if url.userinfo:630 url = url.copy_with(username=None, password=None)631 auto_headers.append((b"host", url.authority.encode("ascii")))632 if not has_content_length and self.method in ("POST", "PUT", "PATCH"):633 auto_headers.append((b"content-length", b"0"))634 if not has_user_agent:635 auto_headers.append((b"user-agent", USER_AGENT.encode("ascii")))636 if not has_accept:637 auto_headers.append((b"accept", b"*/*"))
568 unmodified lines
modified:tests/client/test_client.py+16 added
35 unmodified lines
3637 assert response.json()["Custom-header"] == "value"383940def test_build_post_request(server):41 url = server.url.copy_with(path="/echo_headers")42 headers = {"Custom-header": "value"}4344 with httpx.Client() as client:45 request = client.build_request("POST", url)46 request.headers.update(headers)47 response = client.send(request)4849 assert response.status_code == 20050 assert response.url == url5152 assert response.json()["Content-length"] == "0"53 assert response.json()["Custom-header"] == "value"545556def test_post(server):57 with httpx.Client() as client:58 response = client.post(server.url, data=b"Hello, world!")59 assert response.status_code == 200
105 unmodified lines