Skip to content
Myr

advanced.md

This file

Path

docs/advanced.md

480 lines · 15.5 KiB · Markdown

Last changed by

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

History of this file

Take it

The walk stopped after 200 commits: lines older than 9536b76 point at it.

Fail unit tests without 100% coverage (#206)Can Sarıgölbefore adoption9536b76

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

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

lines 12–15why, one step back
12### Usage
1314The 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:15

Fail unit tests without 100% coverage (#206)Can Sarıgölbefore adoption9536b76

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

Drop sync (#544)Tom Christiebefore adoption206c537

lines 17–19why, one step back
17>>> async with httpx.Client() as client:
18... r = await client.get('https://example.com')19...

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

lines 20–26why, one step back
20>>> r
21<Response [200 OK]>22```2324Alternatively, you can explicitly close the connection pool without block-usage using `.close()`:2526```python

Fail unit tests without 100% coverage (#206)Can Sarıgölbefore adoption9536b76

line 27why, one step back
27>>> client = httpx.Client()

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

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

Drop sync (#544)Tom Christiebefore adoption206c537

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

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

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

Drop sync (#544)Tom Christiebefore adoption206c537

lines 31–32why, one step back
31... await client.close()
32...

Fail unit tests without 100% coverage (#206)Can Sarıgölbefore adoption9536b76

lines 33–36why, one step back
33>>> r
34<Response [200 OK]>35```36

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

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

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

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

Drop sync (#544)Tom Christiebefore adoption206c537

lines 48–49why, one step back
48>>> async with httpx.Client(headers=headers) as client:
49... r = await client.get(url)

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

lines 50–56why, one step back
50...
51>>> r.json()['headers']['User-Agent']52'my-app/0.0.1'53```5455!!! note56 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 57why, one step back
57

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

lines 58–65why, one step back
58 - For headers, query parameters and cookies, the values are merged into one.
59 - For all other parameters, the request-level value is used.6061Additionally, `Client` constructor accepts some parameters that aren't available at the request level.6263One particularly useful parameter is `base_url`, which allows you to define a base URL to prepend to all outgoing requests:6465```python

Drop sync (#544)Tom Christiebefore adoption206c537

lines 66–68why, one step back
66>>> async with httpx.Client(base_url='http://httpbin.org') as client:
67... r = await client.get('/headers')68...

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

lines 69–74why, one step back
69>>> r.request.url
70URL('http://httpbin.org/headers')71```7273For a list of all available client-level parameters, see the [`Client` API reference](/api/#client).74

Fail unit tests without 100% coverage (#206)Can Sarıgölbefore adoption9536b76

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

Link to ASGI docs (#577)Tom Christiebefore adoption7d45db0

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

Fail unit tests without 100% coverage (#206)Can Sarıgölbefore adoption9536b76

lines 79–81why, one step back
79
80This is particularly useful for two main use-cases:81

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

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

Fail unit tests without 100% coverage (#206)Can Sarıgölbefore adoption9536b76

line 84why, one step back
84

Drop sync (#544)Tom Christiebefore adoption206c537

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

Fail unit tests without 100% coverage (#206)Can Sarıgölbefore adoption9536b76

lines 86–87why, one step back
86
87```python

Drop sync (#544)Tom Christiebefore adoption206c537

lines 88–90why, one step back
88from starlette.applications import Starlette
89from starlette.responses import HTMLResponse90from starlette.routing import Route

Fail unit tests without 100% coverage (#206)Can Sarıgölbefore adoption9536b76

lines 91–92why, one step back
91
92

Drop sync (#544)Tom Christiebefore adoption206c537

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

Fail unit tests without 100% coverage (#206)Can Sarıgölbefore adoption9536b76

lines 95–96why, one step back
95
96

Drop sync (#544)Tom Christiebefore adoption206c537

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

Fail unit tests without 100% coverage (#206)Can Sarıgölbefore adoption9536b76

line 98why, one step back
98```

Documented netrc support (#225)Can Sarıgölbefore adoption21939fd

line 99why, one step back
99

Drop sync (#544)Tom Christiebefore adoption206c537

lines 100–109why, one step back
100We can make requests directly against the application, like so:
101102```python103>>> import httpx104>>> async with httpx.Client(app=app) as client:105... r = client.get('http://example/')106... assert r.status_code == 200107... assert r.text == "Hello World!"108```109

Link to ASGI docs (#577)Tom Christiebefore adoption7d45db0

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

Drop `raise_app_exceptions` Client argument. (#238)Tom Christiebefore adoptionef9fc0f

line 111why, one step back
111

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

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

Link to ASGI docs (#577)Tom Christiebefore adoption7d45db0

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

Drop sync (#544)Tom Christiebefore adoption206c537

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

Drop `raise_app_exceptions` Client argument. (#238)Tom Christiebefore adoptionef9fc0f

lines 115–118why, one step back
115
116For example:117118```python

Link to ASGI docs (#577)Tom Christiebefore adoption7d45db0

lines 119–121why, one step back
119# Instantiate a client that makes ASGI requests with a client IP of "1.2.3.4",
120# on port 123.121dispatch = httpx.dispatch.ASGIDispatch(app=app, client=("1.2.3.4", 123))

Drop sync (#544)Tom Christiebefore adoption206c537

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

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

line 123why, one step back
123 ...

Drop `raise_app_exceptions` Client argument. (#238)Tom Christiebefore adoptionef9fc0f

line 124why, one step back
124```

Link to ASGI docs (#577)Tom Christiebefore adoption7d45db0

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

Drop `raise_app_exceptions` Client argument. (#238)Tom Christiebefore adoptionef9fc0f

line 128why, one step back
128

Add `build_request` to `Client` (#319)Dobrovolsky Bogdanbefore adoption08edfac

lines 129–134why, one step back
129## Build Request
130131You can use `Client.build_request()` to build a request and132make modifications before sending the request.133134```python

Drop sync (#544)Tom Christiebefore adoption206c537

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

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

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

Drop sync (#544)Tom Christiebefore adoption206c537

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

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

lines 139–140why, one step back
139...
140>>> r

Add `build_request` to `Client` (#319)Dobrovolsky Bogdanbefore adoption08edfac

lines 141–143why, one step back
141<Response [200 OK]>
142```143

Documented netrc support (#225)Can Sarıgölbefore adoption21939fd

lines 144–148why, one step back
144## .netrc Support
145146HTTPX supports .netrc file. In `trust_env=True` cases, if auth parameter is147not defined, HTTPX tries to add auth into request's header from .netrc file.148

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

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

Documented netrc support (#225)Can Sarıgölbefore adoption21939fd

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

Drop sync (#544)Tom Christiebefore adoption206c537

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

Documented netrc support (#225)Can Sarıgölbefore adoption21939fd

lines 157–175why, one step back
157```
158159If `NETRC` environment is empty, HTTPX tries to use default files.160(`~/.netrc`, `~/_netrc`)161162To change `NETRC` environment:163```python164>>> import os165>>> os.environ["NETRC"] = "my_default_folder/.my_netrc"166```167168.netrc file content example:169```170machine netrcexample.org171login example-username172password example-password173174...175```

Configure Proxy from Client (#353)Seth Michael Larsonbefore adoptionb0fa085

line 176why, one step back
176

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

lines 177–182why, one step back
177When using `Client` instances, `trust_env` should be set on the client itself, rather that on the request methods:
178179```python180client = httpx.Client(trust_env=False)181```182

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

lines 183–192why, one step back
183## Unix Domain Sockets
184185You 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.186187Here's an example requesting the Docker Engine API:188189```python190import httpx191192

Drop sync (#544)Tom Christiebefore adoption206c537

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

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

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

Drop sync (#544)Tom Christiebefore adoption206c537

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

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

lines 196–197why, one step back
196```
197

Configure Proxy from Client (#353)Seth Michael Larsonbefore adoptionb0fa085

lines 198–204why, one step back
198## HTTP Proxying
199200HTTPX supports setting up proxies the same way that Requests does via the `proxies` parameter.201For example to forward all HTTP traffic to `http://127.0.0.1:3080` and all HTTPS traffic202to `http://127.0.0.1:3081` your `proxies` config would look like this:203204```python

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

lines 205–208why, one step back
205>>> proxies = {
206... "http": "http://127.0.0.1:3080",207... "https": "http://127.0.0.1:3081"208... }

Drop sync (#544)Tom Christiebefore adoption206c537

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

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

line 210why, one step back
210... ...

Configure Proxy from Client (#353)Seth Michael Larsonbefore adoptionb0fa085

lines 211–217why, one step back
211```
212213Proxies can be configured for a specific scheme and host, all schemes of a host,214all hosts for a scheme, or for all requests. When determining which proxy configuration215to use for a given request this same order is used.216217```python

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

lines 218–223why, one step back
218>>> proxies = {
219... "http://example.com": "...", # Host+Scheme220... "all://example.com": "...", # Host221... "http": "...", # Scheme222... "all": "...", # All223... }

Drop sync (#544)Tom Christiebefore adoption206c537

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

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

line 225why, one step back
225... ...

Drop sync (#544)Tom Christiebefore adoption206c537

line 226why, one step back
226...

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

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

Drop sync (#544)Tom Christiebefore adoption206c537

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

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

line 229why, one step back
229... ...

Configure Proxy from Client (#353)Seth Michael Larsonbefore adoptionb0fa085

lines 230–247why, one step back
230```
231232!!! warning233 To make sure that proxies cannot read your traffic,234 and even if the proxy_url uses HTTPS, it is recommended to235 use HTTPS and tunnel requests if possible.236237By default `HTTPProxy` will operate as a forwarding proxy for `http://...` requests238and will establish a `CONNECT` TCP tunnel for `https://` requests. This doesn't change239regardless of the `proxy_url` being `http` or `https`.240241Proxies can be configured to have different behavior such as forwarding or tunneling all requests:242243```python244proxy = httpx.HTTPProxy(245 proxy_url="https://127.0.0.1",246 proxy_mode=httpx.HTTPProxyMode.TUNNEL_ONLY247)

Drop sync (#544)Tom Christiebefore adoption206c537

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

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

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

Drop sync (#544)Tom Christiebefore adoption206c537

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

Configure Proxy from Client (#353)Seth Michael Larsonbefore adoptionb0fa085

line 251why, one step back
251```

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

line 252why, one step back
252

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

lines 253–257why, one step back
253!!! note
254255 Per request proxy configuration, i.e. `client.get(url, proxies=...)`,256 has not been implemented yet. To use proxies you must pass the proxy257 information at `Client` initialization.

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

line 258why, one step back
258

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

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

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

line 260why, one step back
260

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

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

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

line 262why, one step back
262

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

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

Multipart files tweaks (#482)Yeray Diaz Diazbefore adoption9ec2cfc

line 265why, one step back
265

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

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

Multipart files tweaks (#482)Yeray Diaz Diazbefore adoption9ec2cfc

line 267why, one step back
267

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

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

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

lines 269–270why, one step back
269
270```python

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

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

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

line 273why, one step back
273

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

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

Drop sync (#544)Tom Christiebefore adoption206c537

line 275why, one step back
275async with httpx.Client() as client:

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

line 276why, one step back
276 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 277–278why, one step back
277```
278

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

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

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

lines 280–281why, one step back
280
281```python

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

lines 282–287why, one step back
282# Using the top-level API:
283await httpx.get('http://example.com/api/v1/example', timeout=None)284285# Using a client instance:286async with httpx.Client() as client:287 await client.get("http://example.com/api/v1/example", timeout=None)

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

lines 288–289why, one step back
288```
289

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

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

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

line 291why, one step back
291

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

lines 292–293why, one step back
292You can set a timeout on a client instance, which results in the given
293`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 294–295why, one step back
294
295```python

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

lines 296–298why, one step back
296client = httpx.Client() # Use a default 5s timeout everywhere.
297client = httpx.Client(timeout=10.0) # Use a default 10s timeout everywhere.298client = httpx.Client(timeout=None) # Disable all timeouts by default.

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

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

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

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

Multipart files tweaks (#482)Yeray Diaz Diazbefore adoption9ec2cfc

line 302why, one step back
302

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

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

Multipart files tweaks (#482)Yeray Diaz Diazbefore adoption9ec2cfc

line 304why, one step back
304

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

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

Multipart files tweaks (#482)Yeray Diaz Diazbefore adoption9ec2cfc

line 307why, one step back
307

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

lines 308–321why, one step back
308* The **connect timeout** specifies the maximum amount of time to wait until
309a connection to the requested host is established. If HTTPX is unable to connect310within this time frame, a `ConnectTimeout` exception is raised.311* The **read timeout** specifies the maximum duration to wait for a chunk of312data to be received (for example, a chunk of the response body). If HTTPX is313unable to receive data within this time frame, a `ReadTimeout` exception is raised.314* The **write timeout** specifies the maximum duration to wait for a chunk of315data to be sent (for example, a chunk of the request body). If HTTPX is unable316to send data within this time frame, a `WriteTimeout` exception is raised.317* The **pool timeout** specifies the maximum duration to wait for acquiring318a connection from the connection pool. If HTTPX is unable to acquire a connection319within this time frame, a `PoolTimeout` exception is raised. A related320configuration here is the maximum number of allowable connections in the321connection pool, which is configured by the `pool_limits`.

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

line 322why, one step back
322

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

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

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

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

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

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

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

line 329why, one step back
329

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

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

Multipart files tweaks (#482)Yeray Diaz Diazbefore adoption9ec2cfc

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

Clarify multipart documentation (#580)Mattwmaster58before adoption33cb397

line 337why, one step back
337name 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 338–340why, one step back
338
339```python340>>> files = {'upload-file': ('report.xls', open('report.xls', 'rb'), 'application/vnd.ms-excel')}

Drop sync (#544)Tom Christiebefore adoption206c537

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

Multipart files tweaks (#482)Yeray Diaz Diazbefore adoption9ec2cfc

lines 342–351why, one step back
342>>> print(r.text)
343{344 ...345 "files": {346 "upload-file": "<... binary content ...>"347 },348 ...349}350```351

Clarify multipart documentation (#580)Mattwmaster58before adoption33cb397

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

Update advanced.mdTom Christiebefore adoption871b0b5

line 353why, one step back
353

Clarify multipart documentation (#580)Mattwmaster58before adoption33cb397

lines 354–355why, one step back
354- The first element is an optional file name which can be set to `None`.
355- 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 356why, one step back
356encoded in UTF-8.

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

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

Multipart files tweaks (#482)Yeray Diaz Diazbefore adoption9ec2cfc

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

Clarify multipart documentation (#580)Mattwmaster58before adoption33cb397

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

Multipart files tweaks (#482)Yeray Diaz Diazbefore adoption9ec2cfc

lines 363–365why, one step back
363
364```python365>>> files = {'upload-file': (None, 'text content', 'text/plain')}

Drop sync (#544)Tom Christiebefore adoption206c537

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

Multipart files tweaks (#482)Yeray Diaz Diazbefore adoption9ec2cfc

lines 367–375why, one step back
367>>> print(r.text)
368{369 ...370 "files": {},371 "form": {372 "upload-file": "text-content"373 },374 ...375}

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

line 376why, one step back
376```

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

lines 377–381why, one step back
377
378## SSL certificates379380When 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).381

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

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

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

lines 383–385why, one step back
383
384By 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.385

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

line 386why, one step back
386If 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 387–390why, one step back
387
388```python389import httpx390

Drop sync (#544)Tom Christiebefore adoption206c537

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

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

line 392why, one step back
392```

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

lines 393–410why, one step back
393
394You can also disable the SSL verification...395396```python397import httpx398399r = await httpx.get("https://example.org", verify=False)400```401402### SSL configuration on client instances403404If you're using a `Client()` instance, then you should pass any SSL settings when instantiating the client.405406```python407client = httpx.Client(verify=False)408```409410The `client.get(...)` method and other request methods *do not* 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 411–423why, one step back
411
412### Making HTTPS requests to a local server413414When making requests to local servers, such as a development server running on `localhost`, you will typically be using unencrypted HTTP connections.415416If 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:4174181. Use [trustme-cli](https://github.com/sethmlarson/trustme-cli/) to generate a pair of server key/cert files, and a client cert file.4191. 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.)4201. Tell HTTPX to use the certificates stored in `client.pem`:421422```python423>>> import httpx

Drop sync (#544)Tom Christiebefore adoption206c537

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

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

lines 425–427why, one step back
425>>> r
426Response <200 OK>427```

Drop sync (#544)Tom Christiebefore adoption206c537

line 428why, one step back
428

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

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

Drop sync (#544)Tom Christiebefore adoption206c537

line 430why, one step back
430

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

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

Drop sync (#544)Tom Christiebefore adoption206c537

line 432why, one step back
432

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

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

Drop sync (#544)Tom Christiebefore adoption206c537

line 435why, one step back
435

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

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

Drop sync (#544)Tom Christiebefore adoption206c537

line 438why, one step back
438

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

lines 439–443why, one step back
439```python
440client = httpx.Client(backend='auto') # Autodetection. The default case.441client = httpx.Client(backend='asyncio') # Use asyncio as the backend.442client = httpx.Client(backend='trio') # Use trio as the backend.443```

Drop sync (#544)Tom Christiebefore adoption206c537

line 444why, one step back
444

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

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

Drop sync (#544)Tom Christiebefore adoption206c537

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

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

line 451why, one step back
451import asyncio

Drop sync (#544)Tom Christiebefore adoption206c537

lines 452–454why, one step back
452import httpx
453454async def main():

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

lines 455–457why, one step back
455 client = httpx.Client()
456 response = await client.get('https://www.example.com/')457 print(response)

Drop sync (#544)Tom Christiebefore adoption206c537

line 458why, one step back
458

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

lines 459–475why, one step back
459asyncio.run(main())
460```461462### [Trio](https://github.com/python-trio/trio)463464Trio is [an alternative async library](https://trio.readthedocs.io/en/stable/),465designed around the [the principles of structured concurrency](https://en.wikipedia.org/wiki/Structured_concurrency).466467```python468import httpx469import trio470471async def main():472 client = httpx.Client()473 response = await client.get('https://www.example.com/')474 print(response)475

Drop sync (#544)Tom Christiebefore adoption206c537

lines 476–479why, one step back
476trio.run(main)
477```478479!!! important

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

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