Skip to content
Myr

advanced.md

This file

Path

docs/advanced.md

435 lines · 13.6 KiB · Markdown

Last changed by

TimeoutConfig -> Timeout (#591)Tom Christiebefore adoptionc033ed1

History of this file

Take it

The walk stopped after 200 commits: lines older than 3ba2e8c point at it.

Switch hosts on HSTS preload to HTTPS (#151)Seth Michael Larsonbefore adoption3ba2e8c

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

Switch hosts on HSTS preload to HTTPS (#151)Seth Michael Larsonbefore adoption3ba2e8c

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

Switch hosts on HSTS preload to HTTPS (#151)Seth Michael Larsonbefore adoption3ba2e8c

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...

Switch hosts on HSTS preload to HTTPS (#151)Seth Michael Larsonbefore adoption3ba2e8c

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

Switch hosts on HSTS preload to HTTPS (#151)Seth Michael Larsonbefore adoption3ba2e8c

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.

Switch hosts on HSTS preload to HTTPS (#151)Seth Michael Larsonbefore adoption3ba2e8c

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.

Switch hosts on HSTS preload to HTTPS (#151)Seth Michael Larsonbefore adoption3ba2e8c

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:

Switch hosts on HSTS preload to HTTPS (#151)Seth Michael Larsonbefore adoption3ba2e8c

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

Switch hosts on HSTS preload to HTTPS (#151)Seth Michael Larsonbefore adoption3ba2e8c

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!")

Switch hosts on HSTS preload to HTTPS (#151)Seth Michael Larsonbefore adoption3ba2e8c

lines 95–96why, one step back
95
96

Drop sync (#544)Tom Christiebefore adoption206c537

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

Switch hosts on HSTS preload to HTTPS (#151)Seth Michael Larsonbefore adoption3ba2e8c

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

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

lines 177–186why, one step back
177## Unix Domain Sockets
178179You 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.180181Here's an example requesting the Docker Engine API:182183```python184import httpx185186

Drop sync (#544)Tom Christiebefore adoption206c537

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

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

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

Drop sync (#544)Tom Christiebefore adoption206c537

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

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

lines 190–191why, one step back
190```
191

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

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

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

lines 199–202why, one step back
199>>> proxies = {
200... "http": "http://127.0.0.1:3080",201... "https": "http://127.0.0.1:3081"202... }

Drop sync (#544)Tom Christiebefore adoption206c537

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

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

line 204why, one step back
204... ...

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

lines 205–211why, one step back
205```
206207Proxies can be configured for a specific scheme and host, all schemes of a host,208all hosts for a scheme, or for all requests. When determining which proxy configuration209to use for a given request this same order is used.210211```python

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

lines 212–217why, one step back
212>>> proxies = {
213... "http://example.com": "...", # Host+Scheme214... "all://example.com": "...", # Host215... "http": "...", # Scheme216... "all": "...", # All217... }

Drop sync (#544)Tom Christiebefore adoption206c537

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

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

line 219why, one step back
219... ...

Drop sync (#544)Tom Christiebefore adoption206c537

line 220why, one step back
220...

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

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

Drop sync (#544)Tom Christiebefore adoption206c537

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

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

line 223why, one step back
223... ...

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

lines 224–241why, one step back
224```
225226!!! warning227 To make sure that proxies cannot read your traffic,228 and even if the proxy_url uses HTTPS, it is recommended to229 use HTTPS and tunnel requests if possible.230231By default `HTTPProxy` will operate as a forwarding proxy for `http://...` requests232and will establish a `CONNECT` TCP tunnel for `https://` requests. This doesn't change233regardless of the `proxy_url` being `http` or `https`.234235Proxies can be configured to have different behavior such as forwarding or tunneling all requests:236237```python238proxy = httpx.HTTPProxy(239 proxy_url="https://127.0.0.1",240 proxy_mode=httpx.HTTPProxyMode.TUNNEL_ONLY241)

Drop sync (#544)Tom Christiebefore adoption206c537

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

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

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

Drop sync (#544)Tom Christiebefore adoption206c537

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

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

line 245why, one step back
245```

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

line 246why, one step back
246

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

lines 247–251why, one step back
247!!! note
248249 Per request proxy configuration, i.e. `client.get(url, proxies=...)`,250 has not been implemented yet. To use proxies you must pass the proxy251 information at `Client` initialization.

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

lines 252–254why, one step back
252
253## Timeout fine-tuning254

Multipart files tweaks (#482)Yeray Diaz Diazbefore adoption9ec2cfc

lines 255–256why, one step back
255HTTPX offers various request timeout management options. Three types of timeouts
256are available: **connect** timeouts, **write** timeouts and **read** timeouts.

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

line 257why, one step back
257

Multipart files tweaks (#482)Yeray Diaz Diazbefore adoption9ec2cfc

lines 258–267why, one step back
258* The **connect timeout** specifies the maximum amount of time to wait until
259a connection to the requested host is established. If HTTPX is unable to connect260within this time frame, a `ConnectTimeout` exception is raised.261* The **write timeout** specifies the maximum duration to wait for a chunk of262data to be sent (for example, a chunk of the request body). If HTTPX is unable263to send data within this time frame, a `WriteTimeout` exception is raised.264* The **read timeout** specifies the maximum duration to wait for a chunk of265data to be received (for example, a chunk of the response body). If HTTPX is266unable to receive data within this time frame, a `ReadTimeout` exception is raised.267

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

line 268why, one step back
268### Setting timeouts

Multipart files tweaks (#482)Yeray Diaz Diazbefore adoption9ec2cfc

line 269why, one step back
269

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

lines 270–275why, one step back
270You can set timeouts on two levels:
271272- For a given request:273274```python275# Using top-level API

Drop sync (#544)Tom Christiebefore adoption206c537

line 276why, one step back
276await httpx.get('http://example.com/api/v1/example', timeout=5)

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

lines 277–278why, one step back
277
278# Or, with a client:

Drop sync (#544)Tom Christiebefore adoption206c537

lines 279–280why, one step back
279async with httpx.Client() as client:
280 await client.get("http://example.com/api/v1/example", timeout=5)

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

lines 281–285why, one step back
281```
282283- On a client instance, which results in the given `timeout` being used as a default for requests made with this client:284285```python

Drop sync (#544)Tom Christiebefore adoption206c537

lines 286–287why, one step back
286async with httpx.Client(timeout=5) as client:
287 await client.get('http://example.com/api/v1/example')

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

lines 288–291why, one step back
288```
289290Besides, you can pass timeouts in two forms:291

Multipart files tweaks (#482)Yeray Diaz Diazbefore adoption9ec2cfc

line 292why, one step back
292- A number, which sets the read, write and connect timeouts to the same value, as in the examples above.

TimeoutConfig -> Timeout (#591)Tom Christiebefore adoptionc033ed1

line 293why, one step back
293- A `Timeout` instance, which allows to define the read, write and connect timeouts independently:

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

lines 294–295why, one step back
294
295```python

TimeoutConfig -> Timeout (#591)Tom Christiebefore adoptionc033ed1

line 296why, one step back
296timeout = httpx.Timeout(

Multipart files tweaks (#482)Yeray Diaz Diazbefore adoption9ec2cfc

lines 297–298why, one step back
297 connect_timeout=5,
298 read_timeout=10,

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

lines 299–301why, one step back
299 write_timeout=15
300)301

Drop sync (#544)Tom Christiebefore adoption206c537

line 302why, one step back
302resp = await httpx.get('http://example.com/api/v1/example', timeout=timeout)

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

lines 303–305why, one step back
303```
304305### Default timeouts

Multipart files tweaks (#482)Yeray Diaz Diazbefore adoption9ec2cfc

line 306why, one step back
306

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

line 307why, one step back
307By default all types of timeouts are set to 5 second.

Multipart files tweaks (#482)Yeray Diaz Diazbefore adoption9ec2cfc

line 308why, one step back
308

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

line 309why, one step back
309### Disabling timeouts

Multipart files tweaks (#482)Yeray Diaz Diazbefore adoption9ec2cfc

lines 310–311why, one step back
310
311To disable timeouts, you can pass `None` as a timeout parameter.

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

lines 312–316why, one step back
312Note that currently this is not supported by the top-level API.
313314```python315url = "http://example.com/api/v1/delay/10"316

Drop sync (#544)Tom Christiebefore adoption206c537

line 317why, one step back
317await httpx.get(url, timeout=None) # Times out after 5s

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

lines 318–319why, one step back
318
319

Drop sync (#544)Tom Christiebefore adoption206c537

lines 320–321why, one step back
320async with httpx.Client(timeout=None) as client:
321 await client.get(url) # Does not timeout, returns after 10s

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

lines 322–323why, one step back
322
323

TimeoutConfig -> Timeout (#591)Tom Christiebefore adoptionc033ed1

line 324why, one step back
324timeout = httpx.Timeout(

Multipart files tweaks (#482)Yeray Diaz Diazbefore adoption9ec2cfc

lines 325–326why, one step back
325 connect_timeout=5,
326 read_timeout=None,

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

lines 327–328why, one step back
327 write_timeout=5
328)

Drop sync (#544)Tom Christiebefore adoption206c537

line 329why, one step back
329await httpx.get(url, timeout=timeout) # Does not timeout, returns after 10s

Multipart files tweaks (#482)Yeray Diaz Diazbefore adoption9ec2cfc

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

Clarify multipart documentation (#580)Mattwmaster58before adoption33cb397

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

Drop sync (#544)Tom Christiebefore adoption206c537

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

Multipart files tweaks (#482)Yeray Diaz Diazbefore adoption9ec2cfc

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

Clarify multipart documentation (#580)Mattwmaster58before adoption33cb397

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

Update advanced.mdTom Christiebefore adoption871b0b5

line 352why, one step back
352

Clarify multipart documentation (#580)Mattwmaster58before adoption33cb397

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

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

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

Multipart files tweaks (#482)Yeray Diaz Diazbefore adoption9ec2cfc

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

Clarify multipart documentation (#580)Mattwmaster58before adoption33cb397

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

Multipart files tweaks (#482)Yeray Diaz Diazbefore adoption9ec2cfc

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

Drop sync (#544)Tom Christiebefore adoption206c537

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

Multipart files tweaks (#482)Yeray Diaz Diazbefore adoption9ec2cfc

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

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

line 375why, one step back
375```

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

lines 376–391why, one step back
376
377## SSL certificates378379When 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).380381### Default CA bundle382383By 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.384385### Using a custom CA bundle386387If you'd like to use a custom CA bundle, you can use the `verify` parameter that is available on the high-level API as well as clients. For example:388389```python390import httpx391

Drop sync (#544)Tom Christiebefore adoption206c537

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

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

lines 393–406why, one step back
393```
394395### Making HTTPS requests to a local server396397When making requests to local servers, such as a development server running on `localhost`, you will typically be using unencrypted HTTP connections.398399If 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:4004011. Use [trustme-cli](https://github.com/sethmlarson/trustme-cli/) to generate a pair of server key/cert files, and a client cert file.4021. 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.)4031. Tell HTTPX to use the certificates stored in `client.pem`:404405```python406>>> import httpx

Drop sync (#544)Tom Christiebefore adoption206c537

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

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

lines 408–410why, one step back
408>>> r
409Response <200 OK>410```

Drop sync (#544)Tom Christiebefore adoption206c537

lines 411–415why, one step back
411
412## Support async environments413414### [asyncio](https://docs.python.org/3/library/asyncio.html) (Default)415

Drop erronous references to AsyncClient (#559)Tom Christiebefore adoption296c9b4

line 416why, one step back
416By default, `Client` uses `asyncio` to perform asynchronous operations and I/O calls.

Drop sync (#544)Tom Christiebefore adoption206c537

lines 417–419why, one step back
417
418### [trio](https://github.com/python-trio/trio)419

Drop erronous references to AsyncClient (#559)Tom Christiebefore adoption296c9b4

line 420why, one step back
420To make asynchronous requests in `trio` programs, pass a `TrioBackend` to the `Client`:

Drop sync (#544)Tom Christiebefore adoption206c537

lines 421–427why, one step back
421
422```python423import trio424import httpx425from httpx.concurrency.trio import TrioBackend426427async def main():

Drop erronous references to AsyncClient (#559)Tom Christiebefore adoption296c9b4

line 428why, one step back
428 async with httpx.Client(backend=TrioBackend()) as client:

Drop sync (#544)Tom Christiebefore adoption206c537

lines 429–435why, one step back
429 ...
430431trio.run(main)432```433434!!! important435 `trio` must be installed to import and use the `TrioBackend`.