diff --git a/httpx/_client.py b/httpx/_client.py index 13cd933673..f8aa7bb031 100644 --- a/httpx/_client.py +++ b/httpx/_client.py @@ -207,7 +207,7 @@ def __init__( self._auth = self._build_auth(auth) self._params = QueryParams(params) - self.headers = Headers(headers) + self.headers = Headers(headers, encoding=default_encoding) self._cookies = Cookies(cookies) self._timeout = Timeout(timeout) self.follow_redirects = follow_redirects diff --git a/httpx/_models.py b/httpx/_models.py index 2cc86321a4..c01f9e4b52 100644 --- a/httpx/_models.py +++ b/httpx/_models.py @@ -272,7 +272,7 @@ def get_list(self, key: str, split_commas: bool = False) -> list[str]: return split_values def update(self, headers: HeaderTypes | None = None) -> None: # type: ignore - headers = Headers(headers) + headers = Headers(headers, encoding=self.encoding) for key in headers.keys(): if key in self: self.pop(key) diff --git a/tests/client/test_headers.py b/tests/client/test_headers.py index 47f5a4d731..ac70a5b834 100755 --- a/tests/client/test_headers.py +++ b/tests/client/test_headers.py @@ -291,3 +291,26 @@ def test_is_not_https_redirect_if_not_default_ports(): headers = client._redirect_headers(request, url, "GET") assert "Authorization" not in headers + + +def test_utf8_header(): + """ + Set a header in the Client. + """ + url = "http://example.org/echo_headers" + headers = {"Example-Header": "заголовок"} + + client = httpx.Client(transport=httpx.MockTransport(echo_headers), headers=headers) + response = client.get(url) + + assert response.status_code == 200 + assert response.json() == { + "headers": { + "accept": "*/*", + "accept-encoding": "gzip, deflate, br, zstd", + "connection": "keep-alive", + "example-header": "заголовок", + "host": "example.org", + "user-agent": f"python-httpx/{httpx.__version__}", + } + }