Skip to content
Myr

advanced.md

This file

Path

docs/advanced.md

485 lines · 16.0 KiB · Markdown

Last changed by

Rename Client to AsyncClient (with compat synonym) (#680)Florimond Mancabefore adoptione284b84

History of this file

Take it

The walk stopped after 200 commits: lines older than 0fa55bb point at it.

Add debug logging to SSLConfig (#378)Seth Michael Larsonbefore adoption0fa55bb

lines 1–8why, one step back
1# Advanced Usage
23## Client Instances45Using a Client instance to make requests will give you HTTP connection pooling,6will provide cookie persistence, and allows you to apply configuration across7all outgoing requests.8

Document client block-usage and close() (#487)Florimond Mancabefore adoption074cd25

lines 9–10why, one step back
9!!! hint
10 A Client instance is equivalent to a Session instance in `requests`.

Add documentation for requests.Session compatibility (#449)Jt Miclatbefore adoption31730e7

line 11why, one step back
11

Rename Client to AsyncClient (with compat synonym) (#680)Florimond Mancabefore adoptione284b84

lines 12–16why, one step back
12!!! note
13 Starting from `httpx==0.10.0`, the default and recommended Client class is `AsyncClient`. This should help prevent breaking changes once sync support is reintroduced.1415 A `Client` synonym remains for compatibility with `httpx==0.9.*` releases, but you are encouraged to migrate to `AsyncClient` as soon as possible.16

Document client block-usage and close() (#487)Florimond Mancabefore adoption074cd25

lines 17–20why, one step back
17### Usage
1819The recommended way to use a `Client` is as a context manager. This will ensure that connections are properly cleaned up when leaving the `with` block:20

Add debug logging to SSLConfig (#378)Seth Michael Larsonbefore adoption0fa55bb

line 21why, one step back
21```python

Rename Client to AsyncClient (with compat synonym) (#680)Florimond Mancabefore adoptione284b84

line 22why, one step back
22>>> async with httpx.AsyncClient() as client:

Drop sync (#544)Tom Christiebefore adoption206c537

lines 23–24why, one step back
23... r = await client.get('https://example.com')
24...

Document client block-usage and close() (#487)Florimond Mancabefore adoption074cd25

lines 25–28why, one step back
25>>> r
26<Response [200 OK]>27```28

Rename 'close' to 'aclose' on Client (#675)Florimond Mancabefore adoptiond5da743

line 29why, one step back
29Alternatively, you can explicitly close the connection pool without block-usage using `.aclose()`:

Document client block-usage and close() (#487)Florimond Mancabefore adoption074cd25

lines 30–31why, one step back
30
31```python

Rename Client to AsyncClient (with compat synonym) (#680)Florimond Mancabefore adoptione284b84

line 32why, one step back
32>>> client = httpx.AsyncClient()

Document client block-usage and close() (#487)Florimond Mancabefore adoption074cd25

line 33why, one step back
33>>> try:

Drop sync (#544)Tom Christiebefore adoption206c537

line 34why, one step back
34... r = await client.get('https://example.com')

Document client block-usage and close() (#487)Florimond Mancabefore adoption074cd25

line 35why, one step back
35... finally:

Rename 'close' to 'aclose' on Client (#675)Florimond Mancabefore adoptiond5da743

line 36why, one step back
36... await client.aclose()

Drop sync (#544)Tom Christiebefore adoption206c537

line 37why, one step back
37...

Add debug logging to SSLConfig (#378)Seth Michael Larsonbefore adoption0fa55bb

lines 38–41why, one step back
38>>> r
39<Response [200 OK]>40```41

Document client block-usage and close() (#487)Florimond Mancabefore adoption074cd25

lines 42–43why, one step back
42Once you have a `Client`, you can use all the features documented in the [Quickstart](/quickstart) guide.
43

Document client-level configuration (#488)Florimond Mancabefore adoptionad38db8

lines 44–52why, one step back
44### Configuration
4546Clients allow you to apply configuration to all outgoing requests by passing parameters to the `Client` constructor.4748For example, to apply a set of custom headers on every request:4950```python51>>> url = 'http://httpbin.org/headers'52>>> headers = {'user-agent': 'my-app/0.0.1'}

Rename Client to AsyncClient (with compat synonym) (#680)Florimond Mancabefore adoptione284b84

line 53why, one step back
53>>> async with httpx.AsyncClient(headers=headers) as client:

Drop sync (#544)Tom Christiebefore adoption206c537

line 54why, one step back
54... r = await client.get(url)

Document client-level configuration (#488)Florimond Mancabefore adoptionad38db8

lines 55–61why, one step back
55...
56>>> r.json()['headers']['User-Agent']57'my-app/0.0.1'58```5960!!! note61 When you provide a parameter at both the client and request levels, one of two things can happen:

Drop sync (#544)Tom Christiebefore adoption206c537

line 62why, one step back
62

Document client-level configuration (#488)Florimond Mancabefore adoptionad38db8

lines 63–65why, one step back
63 - For headers, query parameters and cookies, the values are merged into one.
64 - For all other parameters, the request-level value is used.65

Rename Client to AsyncClient (with compat synonym) (#680)Florimond Mancabefore adoptione284b84

line 66why, one step back
66Additionally, `Client` accepts some parameters that aren't available at the request level.

Document client-level configuration (#488)Florimond Mancabefore adoptionad38db8

lines 67–70why, one step back
67
68One particularly useful parameter is `base_url`, which allows you to define a base URL to prepend to all outgoing requests:6970```python

Rename Client to AsyncClient (with compat synonym) (#680)Florimond Mancabefore adoptione284b84

line 71why, one step back
71>>> async with httpx.AsyncClient(base_url='http://httpbin.org') as client:

Drop sync (#544)Tom Christiebefore adoption206c537

lines 72–73why, one step back
72... r = await client.get('/headers')
73...

Document client-level configuration (#488)Florimond Mancabefore adoptionad38db8

lines 74–77why, one step back
74>>> r.request.url
75URL('http://httpbin.org/headers')76```77

Rename Client to AsyncClient (with compat synonym) (#680)Florimond Mancabefore adoptione284b84

line 78why, one step back
78For a list of all available client-level parameters, see the [`AsyncClient` API reference](/api/#async-client).

Document client-level configuration (#488)Florimond Mancabefore adoptionad38db8

line 79why, one step back
79

Add debug logging to SSLConfig (#378)Seth Michael Larsonbefore adoption0fa55bb

lines 80–82why, one step back
80## Calling into Python Web Apps
8182You can configure an `httpx` client to call directly into a Python web

Link to ASGI docs (#577)Tom Christiebefore adoption7d45db0

line 83why, one step back
83application using the ASGI protocol.

Add debug logging to SSLConfig (#378)Seth Michael Larsonbefore adoption0fa55bb

lines 84–86why, one step back
84
85This is particularly useful for two main use-cases:86

Fix some grammar in Advanced docs (#461)thebigmunchbefore adoption3917866

lines 87–88why, one step back
87* Using `httpx` as a client inside test cases.
88* Mocking out external services during tests or in dev/staging environments.

Add debug logging to SSLConfig (#378)Seth Michael Larsonbefore adoption0fa55bb

line 89why, one step back
89

Drop sync (#544)Tom Christiebefore adoption206c537

line 90why, one step back
90Let's take this Starlette application as an example:

Add debug logging to SSLConfig (#378)Seth Michael Larsonbefore adoption0fa55bb

lines 91–92why, one step back
91
92```python

Drop sync (#544)Tom Christiebefore adoption206c537

lines 93–95why, one step back
93from starlette.applications import Starlette
94from starlette.responses import HTMLResponse95from starlette.routing import Route

Add debug logging to SSLConfig (#378)Seth Michael Larsonbefore adoption0fa55bb

lines 96–97why, one step back
96
97

Drop sync (#544)Tom Christiebefore adoption206c537

lines 98–99why, one step back
98async def hello():
99 return HTMLResponse("Hello World!")

Add debug logging to SSLConfig (#378)Seth Michael Larsonbefore adoption0fa55bb

lines 100–101why, one step back
100
101

Drop sync (#544)Tom Christiebefore adoption206c537

line 102why, one step back
102app = Starlette(routes=[Route("/", hello)])

Add debug logging to SSLConfig (#378)Seth Michael Larsonbefore adoption0fa55bb

lines 103–104why, one step back
103```
104

Drop sync (#544)Tom Christiebefore adoption206c537

lines 105–108why, one step back
105We can make requests directly against the application, like so:
106107```python108>>> import httpx

Rename Client to AsyncClient (with compat synonym) (#680)Florimond Mancabefore adoptione284b84

line 109why, one step back
109>>> async with httpx.AsyncClient(app=app) as client:

Drop sync (#544)Tom Christiebefore adoption206c537

lines 110–114why, one step back
110... r = client.get('http://example/')
111... assert r.status_code == 200112... assert r.text == "Hello World!"113```114

Link to ASGI docs (#577)Tom Christiebefore adoption7d45db0

line 115why, one step back
115For some more complex cases you might need to customise the ASGI dispatch. This allows you to:

Add debug logging to SSLConfig (#378)Seth Michael Larsonbefore adoption0fa55bb

line 116why, one step back
116

Fix some grammar in Advanced docs (#461)thebigmunchbefore adoption3917866

line 117why, one step back
117* Inspect 500 error responses rather than raise exceptions by setting `raise_app_exceptions=False`.

Link to ASGI docs (#577)Tom Christiebefore adoption7d45db0

line 118why, one step back
118* Mount the ASGI application at a subpath by setting `root_path`.

Drop sync (#544)Tom Christiebefore adoption206c537

line 119why, one step back
119* Use a given client address for requests by setting `client`.

Add debug logging to SSLConfig (#378)Seth Michael Larsonbefore adoption0fa55bb

lines 120–123why, one step back
120
121For example:122123```python

Link to ASGI docs (#577)Tom Christiebefore adoption7d45db0

lines 124–126why, one step back
124# Instantiate a client that makes ASGI requests with a client IP of "1.2.3.4",
125# on port 123.126dispatch = httpx.dispatch.ASGIDispatch(app=app, client=("1.2.3.4", 123))

Rename Client to AsyncClient (with compat synonym) (#680)Florimond Mancabefore adoptione284b84

line 127why, one step back
127async with httpx.AsyncClient(dispatch=dispatch) as client:

Document client block-usage and close() (#487)Florimond Mancabefore adoption074cd25

line 128why, one step back
128 ...

Add debug logging to SSLConfig (#378)Seth Michael Larsonbefore adoption0fa55bb

line 129why, one step back
129```

Link to ASGI docs (#577)Tom Christiebefore adoption7d45db0

lines 130–132why, one step back
130
131See [the ASGI documentation](https://asgi.readthedocs.io/en/latest/specs/www.html#connection-scope) for more details on the `client` and `root_path`132keys.

Add debug logging to SSLConfig (#378)Seth Michael Larsonbefore adoption0fa55bb

lines 133–139why, one step back
133
134## Build Request135136You can use `Client.build_request()` to build a request and137make modifications before sending the request.138139```python

Rename Client to AsyncClient (with compat synonym) (#680)Florimond Mancabefore adoptione284b84

line 140why, one step back
140>>> async with httpx.AsyncClient() as client:

Document client block-usage and close() (#487)Florimond Mancabefore adoption074cd25

lines 141–142why, one step back
141... req = client.build_request("OPTIONS", "https://example.com")
142... req.url.full_path = "*" # Build an 'OPTIONS *' request for CORS

Drop sync (#544)Tom Christiebefore adoption206c537

line 143why, one step back
143... r = await client.send(req)

Document client block-usage and close() (#487)Florimond Mancabefore adoption074cd25

lines 144–145why, one step back
144...
145>>> r

Add debug logging to SSLConfig (#378)Seth Michael Larsonbefore adoption0fa55bb

lines 146–153why, one step back
146<Response [200 OK]>
147```148149## .netrc Support150151HTTPX supports .netrc file. In `trust_env=True` cases, if auth parameter is152not defined, HTTPX tries to add auth into request's header from .netrc file.153

Cache netrc authentication per-client (#400)Can Sarıgölbefore adoption84731c8

lines 154–158why, one step back
154!!! note
155 The NETRC file is cached across requests made by a client.156 If you need to refresh the cache (e.g. because the NETRC file has changed),157 you should create a new client or restart the interpreter.158

Add debug logging to SSLConfig (#378)Seth Michael Larsonbefore adoption0fa55bb

lines 159–160why, one step back
159As default `trust_env` is true. To set false:
160```python

Drop sync (#544)Tom Christiebefore adoption206c537

line 161why, one step back
161>>> await httpx.get('https://example.org/', trust_env=False)

Add debug logging to SSLConfig (#378)Seth Michael Larsonbefore adoption0fa55bb

lines 162–181why, one step back
162```
163164If `NETRC` environment is empty, HTTPX tries to use default files.165(`~/.netrc`, `~/_netrc`)166167To change `NETRC` environment:168```python169>>> import os170>>> os.environ["NETRC"] = "my_default_folder/.my_netrc"171```172173.netrc file content example:174```175machine netrcexample.org176login example-username177password example-password178179...180```181

Warn if cert / verify / trust_env are passed to client.request() (#597)Tom Christiebefore adoption1e23855

lines 182–184why, one step back
182When using `Client` instances, `trust_env` should be set on the client itself, rather that on the request methods:
183184```python

Rename Client to AsyncClient (with compat synonym) (#680)Florimond Mancabefore adoptione284b84

line 185why, one step back
185client = httpx.AsyncClient(trust_env=False)

Warn if cert / verify / trust_env are passed to client.request() (#597)Tom Christiebefore adoption1e23855

lines 186–187why, one step back
186```
187

Add Unix Domain Sockets section to advanced docs page (#542)Jonas Lundbergbefore adoptionf0e6acb

lines 188–197why, one step back
188## Unix Domain Sockets
189190You can configure an `httpx` client to connect through a unix domain socket via the `uds` parameter. This is useful when making requests to a server that is bound to a socket file rather than an IP address.191192Here's an example requesting the Docker Engine API:193194```python195import httpx196197

Rename Client to AsyncClient (with compat synonym) (#680)Florimond Mancabefore adoptione284b84

line 198why, one step back
198async with httpx.AsyncClient(uds="/var/run/docker.sock") as client:

Add Unix Domain Sockets section to advanced docs page (#542)Jonas Lundbergbefore adoptionf0e6acb

line 199why, one step back
199 # This request will connect through the socket file.

Drop sync (#544)Tom Christiebefore adoption206c537

line 200why, one step back
200 resp = await client.get("http://localhost/version")

Add Unix Domain Sockets section to advanced docs page (#542)Jonas Lundbergbefore adoptionf0e6acb

lines 201–202why, one step back
201```
202

Add debug logging to SSLConfig (#378)Seth Michael Larsonbefore adoption0fa55bb

lines 203–209why, one step back
203## HTTP Proxying
204205HTTPX supports setting up proxies the same way that Requests does via the `proxies` parameter.206For example to forward all HTTP traffic to `http://127.0.0.1:3080` and all HTTPS traffic207to `http://127.0.0.1:3081` your `proxies` config would look like this:208209```python

Document client block-usage and close() (#487)Florimond Mancabefore adoption074cd25

lines 210–213why, one step back
210>>> proxies = {
211... "http": "http://127.0.0.1:3080",212... "https": "http://127.0.0.1:3081"213... }

Rename Client to AsyncClient (with compat synonym) (#680)Florimond Mancabefore adoptione284b84

line 214why, one step back
214>>> async with httpx.AsyncClient(proxies=proxies) as client:

Document client block-usage and close() (#487)Florimond Mancabefore adoption074cd25

line 215why, one step back
215... ...

Add debug logging to SSLConfig (#378)Seth Michael Larsonbefore adoption0fa55bb

lines 216–222why, one step back
216```
217218Proxies can be configured for a specific scheme and host, all schemes of a host,219all hosts for a scheme, or for all requests. When determining which proxy configuration220to use for a given request this same order is used.221222```python

Document client block-usage and close() (#487)Florimond Mancabefore adoption074cd25

lines 223–228why, one step back
223>>> proxies = {
224... "http://example.com": "...", # Host+Scheme225... "all://example.com": "...", # Host226... "http": "...", # Scheme227... "all": "...", # All228... }

Rename Client to AsyncClient (with compat synonym) (#680)Florimond Mancabefore adoptione284b84

line 229why, one step back
229>>> async with httpx.AsyncClient(proxies=proxies) as client:

Document client block-usage and close() (#487)Florimond Mancabefore adoption074cd25

line 230why, one step back
230... ...

Drop sync (#544)Tom Christiebefore adoption206c537

line 231why, one step back
231...

Document client block-usage and close() (#487)Florimond Mancabefore adoption074cd25

line 232why, one step back
232>>> proxy = "..." # Shortcut for {'all': '...'}

Rename Client to AsyncClient (with compat synonym) (#680)Florimond Mancabefore adoptione284b84

line 233why, one step back
233>>> async with httpx.AsyncClient(proxies=proxy) as client:

Document client block-usage and close() (#487)Florimond Mancabefore adoption074cd25

line 234why, one step back
234... ...

Add debug logging to SSLConfig (#378)Seth Michael Larsonbefore adoption0fa55bb

lines 235–250why, one step back
235```
236237!!! warning238 To make sure that proxies cannot read your traffic,239 and even if the proxy_url uses HTTPS, it is recommended to240 use HTTPS and tunnel requests if possible.241242By default `HTTPProxy` will operate as a forwarding proxy for `http://...` requests243and will establish a `CONNECT` TCP tunnel for `https://` requests. This doesn't change244regardless of the `proxy_url` being `http` or `https`.245246Proxies can be configured to have different behavior such as forwarding or tunneling all requests:247248```python249proxy = httpx.HTTPProxy(250 proxy_url="https://127.0.0.1",

Tighten up top-level API to only expose public API (#608)Tom Christiebefore adoptiond15dc0b

line 251why, one step back
251 proxy_mode="TUNNEL_ONLY" # May be "TUNNEL_ONLY" or "FORWARD_ONLY". Defaults to "DEFAULT".

Add debug logging to SSLConfig (#378)Seth Michael Larsonbefore adoption0fa55bb

line 252why, one step back
252)

Rename Client to AsyncClient (with compat synonym) (#680)Florimond Mancabefore adoptione284b84

line 253why, one step back
253async with httpx.AsyncClient(proxies=proxy) as client:

Document client block-usage and close() (#487)Florimond Mancabefore adoption074cd25

line 254why, one step back
254 # This request will be tunneled instead of forwarded.

Drop sync (#544)Tom Christiebefore adoption206c537

line 255why, one step back
255 r = await client.get("http://example.com")

Add debug logging to SSLConfig (#378)Seth Michael Larsonbefore adoption0fa55bb

line 256why, one step back
256```

Add timeout fine-tunning advanced docs section (#476)Mateusz Wośbefore adoption2984499

line 257why, one step back
257

Drop proxies parameter from the high level API (#485)Yeray Diaz Diazbefore adoption09db6ec

lines 258–262why, one step back
258!!! note
259260 Per request proxy configuration, i.e. `client.get(url, proxies=...)`,261 has not been implemented yet. To use proxies you must pass the proxy262 information at `Client` initialization.

Add timeout fine-tunning advanced docs section (#476)Mateusz Wośbefore adoption2984499

line 263why, one step back
263

Allow default+override timeout style (#593)Tom Christiebefore adoption2f54b20

line 264why, one step back
264## Timeout Configuration

Add timeout fine-tunning advanced docs section (#476)Mateusz Wośbefore adoption2984499

line 265why, one step back
265

Allow default+override timeout style (#593)Tom Christiebefore adoption2f54b20

line 266why, one step back
266HTTPX is careful to enforce timeouts everywhere by default.

Add timeout fine-tunning advanced docs section (#476)Mateusz Wośbefore adoption2984499

line 267why, one step back
267

Allow default+override timeout style (#593)Tom Christiebefore adoption2f54b20

lines 268–269why, one step back
268The default behavior is to raise a `TimeoutException` after 5 seconds of
269network inactivity.

Multipart files tweaks (#482)Yeray Diaz Diazbefore adoption9ec2cfc

line 270why, one step back
270

Allow default+override timeout style (#593)Tom Christiebefore adoption2f54b20

line 271why, one step back
271### Setting and disabling timeouts

Multipart files tweaks (#482)Yeray Diaz Diazbefore adoption9ec2cfc

line 272why, one step back
272

Allow default+override timeout style (#593)Tom Christiebefore adoption2f54b20

line 273why, one step back
273You can set timeouts for an individual request:

Add timeout fine-tunning advanced docs section (#476)Mateusz Wośbefore adoption2984499

lines 274–275why, one step back
274
275```python

Allow default+override timeout style (#593)Tom Christiebefore adoption2f54b20

lines 276–277why, one step back
276# Using the top-level API:
277await httpx.get('http://example.com/api/v1/example', timeout=10.0)

Add timeout fine-tunning advanced docs section (#476)Mateusz Wośbefore adoption2984499

line 278why, one step back
278

Allow default+override timeout style (#593)Tom Christiebefore adoption2f54b20

line 279why, one step back
279# Using a client instance:

Rename Client to AsyncClient (with compat synonym) (#680)Florimond Mancabefore adoptione284b84

line 280why, one step back
280async with httpx.AsyncClient() as client:

Allow default+override timeout style (#593)Tom Christiebefore adoption2f54b20

line 281why, one step back
281 await client.get("http://example.com/api/v1/example", timeout=10.0)

Add timeout fine-tunning advanced docs section (#476)Mateusz Wośbefore adoption2984499

lines 282–283why, one step back
282```
283

Allow default+override timeout style (#593)Tom Christiebefore adoption2f54b20

line 284why, one step back
284Or disable timeouts for an individual request:

Add timeout fine-tunning advanced docs section (#476)Mateusz Wośbefore adoption2984499

lines 285–286why, one step back
285
286```python

Allow default+override timeout style (#593)Tom Christiebefore adoption2f54b20

lines 287–290why, one step back
287# Using the top-level API:
288await httpx.get('http://example.com/api/v1/example', timeout=None)289290# Using a client instance:

Rename Client to AsyncClient (with compat synonym) (#680)Florimond Mancabefore adoptione284b84

line 291why, one step back
291async with httpx.AsyncClient() as client:

Allow default+override timeout style (#593)Tom Christiebefore adoption2f54b20

line 292why, one step back
292 await client.get("http://example.com/api/v1/example", timeout=None)

Add timeout fine-tunning advanced docs section (#476)Mateusz Wośbefore adoption2984499

lines 293–294why, one step back
293```
294

Allow default+override timeout style (#593)Tom Christiebefore adoption2f54b20

line 295why, one step back
295### Setting a default timeout on a client

Add timeout fine-tunning advanced docs section (#476)Mateusz Wośbefore adoption2984499

line 296why, one step back
296

Allow default+override timeout style (#593)Tom Christiebefore adoption2f54b20

lines 297–298why, one step back
297You can set a timeout on a client instance, which results in the given
298`timeout` being used as the default for requests made with this client:

Add timeout fine-tunning advanced docs section (#476)Mateusz Wośbefore adoption2984499

lines 299–300why, one step back
299
300```python

Rename Client to AsyncClient (with compat synonym) (#680)Florimond Mancabefore adoptione284b84

lines 301–303why, one step back
301client = httpx.AsyncClient() # Use a default 5s timeout everywhere.
302client = httpx.AsyncClient(timeout=10.0) # Use a default 10s timeout everywhere.303client = httpx.AsyncClient(timeout=None) # Disable all timeouts by default.

Add timeout fine-tunning advanced docs section (#476)Mateusz Wośbefore adoption2984499

lines 304–305why, one step back
304```
305

Allow default+override timeout style (#593)Tom Christiebefore adoption2f54b20

line 306why, one step back
306### Fine tuning the configuration

Multipart files tweaks (#482)Yeray Diaz Diazbefore adoption9ec2cfc

line 307why, one step back
307

Allow default+override timeout style (#593)Tom Christiebefore adoption2f54b20

line 308why, one step back
308HTTPX also allows you to specify the timeout behavior in more fine grained detail.

Multipart files tweaks (#482)Yeray Diaz Diazbefore adoption9ec2cfc

line 309why, one step back
309

Allow default+override timeout style (#593)Tom Christiebefore adoption2f54b20

lines 310–311why, one step back
310There are four different types of timeouts that may occur. These are **connect**,
311**read**, **write**, and **pool** timeouts.

Multipart files tweaks (#482)Yeray Diaz Diazbefore adoption9ec2cfc

line 312why, one step back
312

Allow default+override timeout style (#593)Tom Christiebefore adoption2f54b20

lines 313–326why, one step back
313* The **connect timeout** specifies the maximum amount of time to wait until
314a connection to the requested host is established. If HTTPX is unable to connect315within this time frame, a `ConnectTimeout` exception is raised.316* The **read timeout** specifies the maximum duration to wait for a chunk of317data to be received (for example, a chunk of the response body). If HTTPX is318unable to receive data within this time frame, a `ReadTimeout` exception is raised.319* The **write timeout** specifies the maximum duration to wait for a chunk of320data to be sent (for example, a chunk of the request body). If HTTPX is unable321to send data within this time frame, a `WriteTimeout` exception is raised.322* The **pool timeout** specifies the maximum duration to wait for acquiring323a connection from the connection pool. If HTTPX is unable to acquire a connection324within this time frame, a `PoolTimeout` exception is raised. A related325configuration here is the maximum number of allowable connections in the326connection pool, which is configured by the `pool_limits`.

Add timeout fine-tunning advanced docs section (#476)Mateusz Wośbefore adoption2984499

line 327why, one step back
327

Allow default+override timeout style (#593)Tom Christiebefore adoption2f54b20

lines 328–329why, one step back
328You can configure the timeout behavior for any of these values...
329

Add timeout fine-tunning advanced docs section (#476)Mateusz Wośbefore adoption2984499

line 330why, one step back
330```python

Allow default+override timeout style (#593)Tom Christiebefore adoption2f54b20

lines 331–332why, one step back
331# A client with a 60s timeout for connecting, and a 10s timeout elsewhere.
332timeout = httpx.Timeout(10.0, connect_timeout=60.0)

Rename Client to AsyncClient (with compat synonym) (#680)Florimond Mancabefore adoptione284b84

line 333why, one step back
333client = httpx.AsyncClient(timeout=timeout)

Add timeout fine-tunning advanced docs section (#476)Mateusz Wośbefore adoption2984499

line 334why, one step back
334

Allow default+override timeout style (#593)Tom Christiebefore adoption2f54b20

line 335why, one step back
335response = await client.get('http://example.com/')

Multipart files tweaks (#482)Yeray Diaz Diazbefore adoption9ec2cfc

lines 336–341why, one step back
336```
337338## Multipart file encoding339340As mentioned in the [quickstart](/quickstart#sending-multipart-file-uploads)341multipart file encoding is available by passing a dictionary with the

Clarify multipart documentation (#580)Mattwmaster58before adoption33cb397

line 342why, one step back
342name of the payloads as keys and either tuple of elements or a file-like object or a string as values.

Multipart files tweaks (#482)Yeray Diaz Diazbefore adoption9ec2cfc

lines 343–345why, one step back
343
344```python345>>> files = {'upload-file': ('report.xls', open('report.xls', 'rb'), 'application/vnd.ms-excel')}

Drop sync (#544)Tom Christiebefore adoption206c537

line 346why, one step back
346>>> r = await httpx.post("https://httpbin.org/post", files=files)

Multipart files tweaks (#482)Yeray Diaz Diazbefore adoption9ec2cfc

lines 347–356why, one step back
347>>> print(r.text)
348{349 ...350 "files": {351 "upload-file": "<... binary content ...>"352 },353 ...354}355```356

Clarify multipart documentation (#580)Mattwmaster58before adoption33cb397

line 357why, one step back
357More specifically, if a tuple is used as a value, it must have between 2 and 3 elements:

Update advanced.mdTom Christiebefore adoption871b0b5

line 358why, one step back
358

Clarify multipart documentation (#580)Mattwmaster58before adoption33cb397

lines 359–360why, one step back
359- The first element is an optional file name which can be set to `None`.
360- The second element may be a file-like object or a string which will be automatically

Multipart files tweaks (#482)Yeray Diaz Diazbefore adoption9ec2cfc

line 361why, one step back
361encoded in UTF-8.

Better HTTP/2 defaults. (#584)Tom Christiebefore adoption30229f1

line 362why, one step back
362- An optional third element can be used to specify the

Multipart files tweaks (#482)Yeray Diaz Diazbefore adoption9ec2cfc

line 363why, one step back
363[MIME type](https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/MIME_Types)

Clarify multipart documentation (#580)Mattwmaster58before adoption33cb397

lines 364–367why, one step back
364of the file being uploaded. If not specified HTTPX will attempt to guess the MIME type based
365on the file name, with unknown file extensions defaulting to "application/octet-stream".366If the file name is explicitly set to `None` then HTTPX will not include a content-type367MIME header field.

Multipart files tweaks (#482)Yeray Diaz Diazbefore adoption9ec2cfc

lines 368–370why, one step back
368
369```python370>>> files = {'upload-file': (None, 'text content', 'text/plain')}

Drop sync (#544)Tom Christiebefore adoption206c537

line 371why, one step back
371>>> r = await httpx.post("https://httpbin.org/post", files=files)

Multipart files tweaks (#482)Yeray Diaz Diazbefore adoption9ec2cfc

lines 372–380why, one step back
372>>> print(r.text)
373{374 ...375 "files": {},376 "form": {377 "upload-file": "text-content"378 },379 ...380}

Add timeout fine-tunning advanced docs section (#476)Mateusz Wośbefore adoption2984499

line 381why, one step back
381```

Add docs on SSL certificates (#510)Florimond Mancabefore adoption95b2b24

lines 382–386why, one step back
382
383## SSL certificates384385When making a request over HTTPS, HTTPX needs to verify the identity of the requested host. To do this, it uses a bundle of SSL certificates (a.k.a. CA bundle) delivered by a trusted certificate authority (CA).386

Warn if cert / verify / trust_env are passed to client.request() (#597)Tom Christiebefore adoption1e23855

line 387why, one step back
387### Changing the verification defaults

Add docs on SSL certificates (#510)Florimond Mancabefore adoption95b2b24

lines 388–390why, one step back
388
389By default, HTTPX uses the CA bundle provided by [Certifi](https://pypi.org/project/certifi/). This is what you want in most cases, even though some advanced situations may require you to use a different set of certificates.390

Warn if cert / verify / trust_env are passed to client.request() (#597)Tom Christiebefore adoption1e23855

line 391why, one step back
391If you'd like to use a custom CA bundle, you can use the `verify` parameter.

Add docs on SSL certificates (#510)Florimond Mancabefore adoption95b2b24

lines 392–395why, one step back
392
393```python394import httpx395

Drop sync (#544)Tom Christiebefore adoption206c537

line 396why, one step back
396r = await httpx.get("https://example.org", verify="path/to/client.pem")

Add docs on SSL certificates (#510)Florimond Mancabefore adoption95b2b24

line 397why, one step back
397```

Warn if cert / verify / trust_env are passed to client.request() (#597)Tom Christiebefore adoption1e23855

lines 398–411why, one step back
398
399You can also disable the SSL verification...400401```python402import httpx403404r = await httpx.get("https://example.org", verify=False)405```406407### SSL configuration on client instances408409If you're using a `Client()` instance, then you should pass any SSL settings when instantiating the client.410411```python

Rename Client to AsyncClient (with compat synonym) (#680)Florimond Mancabefore adoptione284b84

line 412why, one step back
412client = httpx.AsyncClient(verify=False)

Warn if cert / verify / trust_env are passed to client.request() (#597)Tom Christiebefore adoption1e23855

lines 413–414why, one step back
413```
414

Update advanced.mdTom Christiebefore adoptionddc4885

line 415why, one step back
415The `client.get(...)` method and other request methods *do not* support changing the SSL settings on a per-request basis. If you need different SSL settings in different cases you should use more that one client instance, with different settings on each. Each client will then be using an isolated connection pool with a specific fixed SSL configuration on all connections within that pool.

Add docs on SSL certificates (#510)Florimond Mancabefore adoption95b2b24

lines 416–428why, one step back
416
417### Making HTTPS requests to a local server418419When making requests to local servers, such as a development server running on `localhost`, you will typically be using unencrypted HTTP connections.420421If you do need to make HTTPS connections to a local server, for example to test an HTTPS-only service, you will need to create and use your own certificates. Here's one way to do it:4224231. Use [trustme-cli](https://github.com/sethmlarson/trustme-cli/) to generate a pair of server key/cert files, and a client cert file.4241. Pass the server key/cert files when starting your local server. (This depends on the particular web server you're using. For example, [Uvicorn](https://www.uvicorn.org) provides the `--ssl-keyfile` and `--ssl-certfile` options.)4251. Tell HTTPX to use the certificates stored in `client.pem`:426427```python428>>> import httpx

Drop sync (#544)Tom Christiebefore adoption206c537

line 429why, one step back
429>>> r = await httpx.get("https://localhost:8000", verify="/tmp/client.pem")

Add docs on SSL certificates (#510)Florimond Mancabefore adoption95b2b24

lines 430–432why, one step back
430>>> r
431Response <200 OK>432```

Drop sync (#544)Tom Christiebefore adoption206c537

line 433why, one step back
433

Improve backend docs, particularly wrt. autodetection (#595)Tom Christiebefore adoptionf8794cb

line 434why, one step back
434## Supported async environments

Drop sync (#544)Tom Christiebefore adoption206c537

line 435why, one step back
435

Improve backend docs, particularly wrt. autodetection (#595)Tom Christiebefore adoptionf8794cb

line 436why, one step back
436HTTPX supports either `asyncio` or `trio` as an async environment.

Drop sync (#544)Tom Christiebefore adoption206c537

line 437why, one step back
437

Improve backend docs, particularly wrt. autodetection (#595)Tom Christiebefore adoptionf8794cb

lines 438–439why, one step back
438By default it will auto-detect which of those two to use as the backend
439for socket operations and concurrency primitives.

Drop sync (#544)Tom Christiebefore adoption206c537

line 440why, one step back
440

Improve backend docs, particularly wrt. autodetection (#595)Tom Christiebefore adoptionf8794cb

lines 441–442why, one step back
441You can also explicitly select a backend by instantiating a client with the
442`backend` argument...

Drop sync (#544)Tom Christiebefore adoption206c537

line 443why, one step back
443

Improve backend docs, particularly wrt. autodetection (#595)Tom Christiebefore adoptionf8794cb

line 444why, one step back
444```python

Rename Client to AsyncClient (with compat synonym) (#680)Florimond Mancabefore adoptione284b84

lines 445–447why, one step back
445client = httpx.AsyncClient(backend='auto') # Autodetection. The default case.
446client = httpx.AsyncClient(backend='asyncio') # Use asyncio as the backend.447client = httpx.AsyncClient(backend='trio') # Use trio as the backend.

Improve backend docs, particularly wrt. autodetection (#595)Tom Christiebefore adoptionf8794cb

line 448why, one step back
448```

Drop sync (#544)Tom Christiebefore adoption206c537

line 449why, one step back
449

Improve backend docs, particularly wrt. autodetection (#595)Tom Christiebefore adoptionf8794cb

lines 450–454why, one step back
450### [AsyncIO](https://docs.python.org/3/library/asyncio.html)
451452AsyncIO is Python's [built-in library](https://docs.python.org/3/library/asyncio.html)453for writing concurrent code with the async/await syntax.454

Drop sync (#544)Tom Christiebefore adoption206c537

line 455why, one step back
455```python

Improve backend docs, particularly wrt. autodetection (#595)Tom Christiebefore adoptionf8794cb

line 456why, one step back
456import asyncio

Drop sync (#544)Tom Christiebefore adoption206c537

lines 457–459why, one step back
457import httpx
458459async def main():

Rename Client to AsyncClient (with compat synonym) (#680)Florimond Mancabefore adoptione284b84

line 460why, one step back
460 client = httpx.AsyncClient()

Improve backend docs, particularly wrt. autodetection (#595)Tom Christiebefore adoptionf8794cb

lines 461–462why, one step back
461 response = await client.get('https://www.example.com/')
462 print(response)

Drop sync (#544)Tom Christiebefore adoption206c537

line 463why, one step back
463

Improve backend docs, particularly wrt. autodetection (#595)Tom Christiebefore adoptionf8794cb

lines 464–476why, one step back
464asyncio.run(main())
465```466467### [Trio](https://github.com/python-trio/trio)468469Trio is [an alternative async library](https://trio.readthedocs.io/en/stable/),470designed around the [the principles of structured concurrency](https://en.wikipedia.org/wiki/Structured_concurrency).471472```python473import httpx474import trio475476async def main():

Rename Client to AsyncClient (with compat synonym) (#680)Florimond Mancabefore adoptione284b84

line 477why, one step back
477 client = httpx.AsyncClient()

Improve backend docs, particularly wrt. autodetection (#595)Tom Christiebefore adoptionf8794cb

lines 478–480why, one step back
478 response = await client.get('https://www.example.com/')
479 print(response)480

Drop sync (#544)Tom Christiebefore adoption206c537

lines 481–484why, one step back
481trio.run(main)
482```483484!!! important

Improve backend docs, particularly wrt. autodetection (#595)Tom Christiebefore adoptionf8794cb

line 485why, one step back
485 The `trio` package must be installed to use the Trio backend.