Repository: encode/httpx

Issue:
Set Headers like Cookies in Client session for all requests
Assuming one can use `Client`/`AsyncClient` as a drop-in replacement for `requests.Session`, I would like to be able to set headers (and cookies) to be sent for each request, for example for token-based authentication.

With `requests`, I can do:
```python
header_dict = {"X-Auth-Token": "FooBarBazToken"}
session = requests.Session()
session.headers.update(header_dict)
```

`cookies` can already be set on the `httpx.Client`. I would like to be able to also do that with `headers`:
```python
>>> headers = Headers({"X-Auth-Token": "FooBarBazToken", "User-Agent": "python-myclient/0.2.1"})
>>> session = Client(base_url="https://httpbin.org", headers=headers)

>>> get_headers = session.get("/headers")

>>> get_headers.json()
{'headers': {'Accept': '*/*',
  'Accept-Encoding': 'gzip, deflate',
  'Cookie': 'MyCookie=MyValue',
  'Host': 'httpbin.org',
  'User-Agent': 'python-myclient/0.2.1',
  'X-Auth-Token': 'FooBarBazToken'}}
```


Discussion:
Indeed, yup! 👍
We've got a `.cookies` interface on there already, but we don't yet have a `.headers` interface.
So there is a `cookies=` option! I missed it because I was searching so hard for `headers`. I'll update the OP.

Resolving patch:
diff --git a/httpx/client.py b/httpx/client.py
--- a/httpx/client.py
+++ b/httpx/client.py
@@ -49,6 +49,7 @@ class BaseClient:
     def __init__(
         self,
         auth: AuthTypes = None,
+        headers: HeaderTypes = None,
         cookies: CookieTypes = None,
         verify: VerifyTypes = True,
         cert: CertTypes = None,
@@ -95,6 +96,7 @@ def __init__(
             self.base_url = URL(base_url)
 
         self.auth = auth
+        self.headers = Headers(headers)
         self.cookies = Cookies(cookies)
         self.max_redirects = max_redirects
         self.dispatch = async_dispatch
@@ -109,6 +111,15 @@ def merge_cookies(
             return merged_cookies
         return cookies
 
+    def merge_headers(
+        self, headers: HeaderTypes = None
+    ) -> typing.Optional[HeaderTypes]:
+        if headers or self.headers:
+            merged_headers = Headers(self.headers)
+            merged_headers.update(headers)
+            return merged_headers
+        return headers
+
     async def send(
         self,
         request: AsyncRequest,
@@ -527,6 +538,7 @@ async def request(
         timeout: TimeoutTypes = None,
     ) -> AsyncResponse:
         url = self.base_url.join(url)
+        headers = self.merge_headers(headers)
         cookies = self.merge_cookies(cookies)
         request = AsyncRequest(
             method,
@@ -608,6 +620,7 @@ def request(
         timeout: TimeoutTypes = None,
     ) -> Response:
         url = self.base_url.join(url)
+        headers = self.merge_headers(headers)
         cookies = self.merge_cookies(cookies)
         request = AsyncRequest(
             method,
diff --git a/httpx/models.py b/httpx/models.py
--- a/httpx/models.py
+++ b/httpx/models.py
@@ -415,6 +415,11 @@ def getlist(self, key: str, split_commas: bool = False) -> typing.List[str]:
             split_values.extend([item.strip() for item in value.split(",")])
         return split_values
 
+    def update(self, headers: HeaderTypes = None) -> None:  # type: ignore
+        headers = Headers(headers)
+        for header in headers:
+            self[header] = headers[header]
+
     def __getitem__(self, key: str) -> str:
         """
         Return a single header value.


Test patch:
diff --git a/tests/client/test_headers.py b/tests/client/test_headers.py
new file mode 100755
--- /dev/null
+++ b/tests/client/test_headers.py
@@ -0,0 +1,123 @@
+#!/usr/bin/env python3
+
+import json
+from httpx import (
+    __version__,
+    Client,
+    AsyncRequest,
+    AsyncResponse,
+    VerifyTypes,
+    CertTypes,
+    TimeoutTypes,
+    AsyncDispatcher,
+)
+
+
+class MockDispatch(AsyncDispatcher):
+    async def send(
+        self,
+        request: AsyncRequest,
+        verify: VerifyTypes = None,
+        cert: CertTypes = None,
+        timeout: TimeoutTypes = None,
+    ) -> AsyncResponse:
+        if request.url.path.startswith("/echo_headers"):
+            request_headers = dict(request.headers.items())
+            body = json.dumps({"headers": request_headers}).encode()
+            return AsyncResponse(200, content=body, request=request)
+
+
+def test_client_header():
+    """
+    Set a header in the Client.
+    """
+    url = "http://example.org/echo_headers"
+    headers = {"Example-Header": "example-value"}
+
+    with Client(dispatch=MockDispatch(), headers=headers) as client:
+        response = client.get(url)
+
+    assert response.status_code == 200
+    assert response.json() == {
+        "headers": {
+            "accept": "*/*",
+            "accept-encoding": "gzip, deflate, br",
+            "connection": "keep-alive",
+            "example-header": "example-value",
+            "host": "example.org",
+            "user-agent": f"python-httpx/{__version__}",
+        }
+    }
+
+
+def test_header_merge():
+    url = "http://example.org/echo_headers"
+    client_headers = {"User-Agent": "python-myclient/0.2.1"}
+    request_headers = {"X-Auth-Token": "FooBarBazToken"}
+    with Client(dispatch=MockDispatch(), headers=client_headers) as client:
+        response = client.get(url, headers=request_headers)
+
+    assert response.status_code == 200
+    assert response.json() == {
+        "headers": {
+            "accept": "*/*",
+            "accept-encoding": "gzip, deflate, br",
+            "connection": "keep-alive",
+            "host": "example.org",
+            "user-agent": "python-myclient/0.2.1",
+            "x-auth-token": "FooBarBazToken",
+        }
+    }
+
+
+def test_header_merge_conflicting_headers():
+    url = "http://example.org/echo_headers"
+    client_headers = {"X-Auth-Token": "FooBar"}
+    request_headers = {"X-Auth-Token": "BazToken"}
+    with Client(dispatch=MockDispatch(), headers=client_headers) as client:
+        response = client.get(url, headers=request_headers)
+
+    assert response.status_code == 200
+    assert response.json() == {
+        "headers": {
+            "accept": "*/*",
+            "accept-encoding": "gzip, deflate, br",
+            "connection": "keep-alive",
+            "host": "example.org",
+            "user-agent": f"python-httpx/{__version__}",
+            "x-auth-token": "BazToken",
+        }
+    }
+
+
+def test_header_update():
+    url = "http://example.org/echo_headers"
+    with Client(dispatch=MockDispatch()) as client:
+        first_response = client.get(url)
+        client.headers.update(
+            {"User-Agent": "python-myclient/0.2.1", "Another-Header": "AThing"}
+        )
+        second_response = client.get(url)
+
+    assert first_response.status_code == 200
+    assert first_response.json() == {
+        "headers": {
+            "accept": "*/*",
+            "accept-encoding": "gzip, deflate, br",
+            "connection": "keep-alive",
+            "host": "example.org",
+            "user-agent": f"python-httpx/{__version__}",
+        }
+    }
+
+    assert second_response.status_code == 200
+    assert second_response.json() == {
+        "headers": {
+            "accept": "*/*",
+            "accept-encoding": "gzip, deflate, br",
+            "another-header": "AThing",
+            "connection": "keep-alive",
+            "host": "example.org",
+            "user-agent": "python-myclient/0.2.1",
+        }
+    }