advanced.md
This file
Path
docs/advanced.md
448 lines · 14.0 KiB · Markdown
Last changed by
Link to ASGI docs (#577)Tom Christiebefore adoption7d45db0
History of this fileTake 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 back1# Advanced Usage23## 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.8Document client block-usage and close() (#487)Florimond Mancabefore adoption074cd25
lines 9–10why, one step back9!!! hint10 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 back11Document client block-usage and close() (#487)Florimond Mancabefore adoption074cd25
lines 12–15why, one step back12### Usage1314The 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:15Switch hosts on HSTS preload to HTTPS (#151)Seth Michael Larsonbefore adoption3ba2e8c
line 16why, one step back16```pythonDrop sync (#544)Tom Christiebefore adoption206c537
lines 17–19why, one step back17>>> 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 back20>>> r21<Response [200 OK]>22```2324Alternatively, you can explicitly close the connection pool without block-usage using `.close()`:2526```pythonSwitch hosts on HSTS preload to HTTPS (#151)Seth Michael Larsonbefore adoption3ba2e8c
line 27why, one step back27>>> client = httpx.Client()Document client block-usage and close() (#487)Florimond Mancabefore adoption074cd25
line 28why, one step back28>>> try:Drop sync (#544)Tom Christiebefore adoption206c537
line 29why, one step back29... r = await client.get('https://example.com')Document client block-usage and close() (#487)Florimond Mancabefore adoption074cd25
line 30why, one step back30... finally:Drop sync (#544)Tom Christiebefore adoption206c537
lines 31–32why, one step back31... await client.close()32...Switch hosts on HSTS preload to HTTPS (#151)Seth Michael Larsonbefore adoption3ba2e8c
lines 33–36why, one step back33>>> r34<Response [200 OK]>35```36Document client block-usage and close() (#487)Florimond Mancabefore adoption074cd25
lines 37–38why, one step back37Once you have a `Client`, you can use all the features documented in the [Quickstart](/quickstart) guide.38Document client-level configuration (#488)Florimond Mancabefore adoptionad38db8
lines 39–47why, one step back39### Configuration4041Clients 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 back48>>> 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 back50...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 back57Document client-level configuration (#488)Florimond Mancabefore adoptionad38db8
lines 58–65why, one step back58 - 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```pythonDrop sync (#544)Tom Christiebefore adoption206c537
lines 66–68why, one step back66>>> 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 back69>>> r.request.url70URL('http://httpbin.org/headers')71```7273For a list of all available client-level parameters, see the [`Client` API reference](/api/#client).74Switch hosts on HSTS preload to HTTPS (#151)Seth Michael Larsonbefore adoption3ba2e8c
lines 75–77why, one step back75## Calling into Python Web Apps7677You can configure an `httpx` client to call directly into a Python webLink to ASGI docs (#577)Tom Christiebefore adoption7d45db0
line 78why, one step back78application using the ASGI protocol.Switch hosts on HSTS preload to HTTPS (#151)Seth Michael Larsonbefore adoption3ba2e8c
lines 79–81why, one step back7980This is particularly useful for two main use-cases:81Fix some grammar in Advanced docs (#461)thebigmunchbefore adoption3917866
lines 82–83why, one step back82* 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 back84Drop sync (#544)Tom Christiebefore adoption206c537
line 85why, one step back85Let'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 back8687```pythonDrop sync (#544)Tom Christiebefore adoption206c537
lines 88–90why, one step back88from starlette.applications import Starlette89from starlette.responses import HTMLResponse90from starlette.routing import RouteSwitch hosts on HSTS preload to HTTPS (#151)Seth Michael Larsonbefore adoption3ba2e8c
lines 91–92why, one step back9192Drop sync (#544)Tom Christiebefore adoption206c537
lines 93–94why, one step back93async def hello():94 return HTMLResponse("Hello World!")Switch hosts on HSTS preload to HTTPS (#151)Seth Michael Larsonbefore adoption3ba2e8c
lines 95–96why, one step back9596Drop sync (#544)Tom Christiebefore adoption206c537
line 97why, one step back97app = Starlette(routes=[Route("/", hello)])Switch hosts on HSTS preload to HTTPS (#151)Seth Michael Larsonbefore adoption3ba2e8c
line 98why, one step back98```Documented netrc support (#225)Can Sarıgölbefore adoption21939fd
line 99why, one step back99Drop sync (#544)Tom Christiebefore adoption206c537
lines 100–109why, one step back100We 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```109Link to ASGI docs (#577)Tom Christiebefore adoption7d45db0
line 110why, one step back110For 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 back111Fix some grammar in Advanced docs (#461)thebigmunchbefore adoption3917866
line 112why, one step back112* 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 back113* Mount the ASGI application at a subpath by setting `root_path`.Drop sync (#544)Tom Christiebefore adoption206c537
line 114why, one step back114* 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 back115116For example:117118```pythonLink to ASGI docs (#577)Tom Christiebefore adoption7d45db0
lines 119–121why, one step back119# 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 back122async with httpx.Client(dispatch=dispatch) as client:Document client block-usage and close() (#487)Florimond Mancabefore adoption074cd25
line 123why, one step back123 ...Drop `raise_app_exceptions` Client argument. (#238)Tom Christiebefore adoptionef9fc0f
line 124why, one step back124```Link to ASGI docs (#577)Tom Christiebefore adoption7d45db0
lines 125–127why, one step back125126See [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 back128Add `build_request` to `Client` (#319)Dobrovolsky Bogdanbefore adoption08edfac
lines 129–134why, one step back129## Build Request130131You can use `Client.build_request()` to build a request and132make modifications before sending the request.133134```pythonDrop sync (#544)Tom Christiebefore adoption206c537
line 135why, one step back135>>> async with httpx.Client() as client:Document client block-usage and close() (#487)Florimond Mancabefore adoption074cd25
lines 136–137why, one step back136... req = client.build_request("OPTIONS", "https://example.com")137... req.url.full_path = "*" # Build an 'OPTIONS *' request for CORSDrop sync (#544)Tom Christiebefore adoption206c537
line 138why, one step back138... r = await client.send(req)Document client block-usage and close() (#487)Florimond Mancabefore adoption074cd25
lines 139–140why, one step back139...140>>> rAdd `build_request` to `Client` (#319)Dobrovolsky Bogdanbefore adoption08edfac
lines 141–143why, one step back141<Response [200 OK]>142```143Fix typos, spelling issues, and grammar in docs (#426)Kyle Galbraithbefore adoptionb65bce5
line 144why, one step back144## Specify the version of the HTTP protocolAdd example of specifying HTTP versions (#337)Paul (xobb) Chubatyybefore adoption0c4460e
line 145why, one step back145Fix typos, spelling issues, and grammar in docs (#426)Kyle Galbraithbefore adoptionb65bce5
line 146why, one step back146One can set the version of the HTTP protocol for the client in case you want to make the requests using a specific version.Add example of specifying HTTP versions (#337)Paul (xobb) Chubatyybefore adoption0c4460e
lines 147–150why, one step back147148For example:149150```pythonDrop sync (#544)Tom Christiebefore adoption206c537
lines 151–152why, one step back151async with httpx.Client(http_versions=["HTTP/1.1"]) as h11_client:152 h11_response = await h11_client.get("https://myserver.com")Add example of specifying HTTP versions (#337)Paul (xobb) Chubatyybefore adoption0c4460e
line 153why, one step back153Drop sync (#544)Tom Christiebefore adoption206c537
lines 154–155why, one step back154async with httpx.Client(http_versions=["HTTP/2"]) as h2_client:155 h2_response = await h2_client.get("https://myserver.com")Add example of specifying HTTP versions (#337)Paul (xobb) Chubatyybefore adoption0c4460e
lines 156–157why, one step back156```157Documented netrc support (#225)Can Sarıgölbefore adoption21939fd
lines 158–162why, one step back158## .netrc Support159160HTTPX supports .netrc file. In `trust_env=True` cases, if auth parameter is161not defined, HTTPX tries to add auth into request's header from .netrc file.162Cache netrc authentication per-client (#400)Can Sarıgölbefore adoption84731c8
lines 163–167why, one step back163!!! note164 The NETRC file is cached across requests made by a client.165 If you need to refresh the cache (e.g. because the NETRC file has changed),166 you should create a new client or restart the interpreter.167Documented netrc support (#225)Can Sarıgölbefore adoption21939fd
lines 168–169why, one step back168As default `trust_env` is true. To set false:169```pythonDrop sync (#544)Tom Christiebefore adoption206c537
line 170why, one step back170>>> await httpx.get('https://example.org/', trust_env=False)Documented netrc support (#225)Can Sarıgölbefore adoption21939fd
lines 171–189why, one step back171```172173If `NETRC` environment is empty, HTTPX tries to use default files.174(`~/.netrc`, `~/_netrc`)175176To change `NETRC` environment:177```python178>>> import os179>>> os.environ["NETRC"] = "my_default_folder/.my_netrc"180```181182.netrc file content example:183```184machine netrcexample.org185login example-username186password example-password187188...189```Configure Proxy from Client (#353)Seth Michael Larsonbefore adoptionb0fa085
line 190why, one step back190Add Unix Domain Sockets section to advanced docs page (#542)Jonas Lundbergbefore adoptionf0e6acb
lines 191–200why, one step back191## Unix Domain Sockets192193You 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.194195Here's an example requesting the Docker Engine API:196197```python198import httpx199200Drop sync (#544)Tom Christiebefore adoption206c537
line 201why, one step back201async with httpx.Client(uds="/var/run/docker.sock") as client:Add Unix Domain Sockets section to advanced docs page (#542)Jonas Lundbergbefore adoptionf0e6acb
line 202why, one step back202 # This request will connect through the socket file.Drop sync (#544)Tom Christiebefore adoption206c537
line 203why, one step back203 resp = await client.get("http://localhost/version")Add Unix Domain Sockets section to advanced docs page (#542)Jonas Lundbergbefore adoptionf0e6acb
lines 204–205why, one step back204```205Configure Proxy from Client (#353)Seth Michael Larsonbefore adoptionb0fa085
lines 206–212why, one step back206## HTTP Proxying207208HTTPX supports setting up proxies the same way that Requests does via the `proxies` parameter.209For example to forward all HTTP traffic to `http://127.0.0.1:3080` and all HTTPS traffic210to `http://127.0.0.1:3081` your `proxies` config would look like this:211212```pythonDocument client block-usage and close() (#487)Florimond Mancabefore adoption074cd25
lines 213–216why, one step back213>>> proxies = {214... "http": "http://127.0.0.1:3080",215... "https": "http://127.0.0.1:3081"216... }Drop sync (#544)Tom Christiebefore adoption206c537
line 217why, one step back217>>> async with httpx.Client(proxies=proxies) as client:Document client block-usage and close() (#487)Florimond Mancabefore adoption074cd25
line 218why, one step back218... ...Configure Proxy from Client (#353)Seth Michael Larsonbefore adoptionb0fa085
lines 219–225why, one step back219```220221Proxies can be configured for a specific scheme and host, all schemes of a host,222all hosts for a scheme, or for all requests. When determining which proxy configuration223to use for a given request this same order is used.224225```pythonDocument client block-usage and close() (#487)Florimond Mancabefore adoption074cd25
lines 226–231why, one step back226>>> proxies = {227... "http://example.com": "...", # Host+Scheme228... "all://example.com": "...", # Host229... "http": "...", # Scheme230... "all": "...", # All231... }Drop sync (#544)Tom Christiebefore adoption206c537
line 232why, one step back232>>> async with httpx.Client(proxies=proxies) as client:Document client block-usage and close() (#487)Florimond Mancabefore adoption074cd25
line 233why, one step back233... ...Drop sync (#544)Tom Christiebefore adoption206c537
line 234why, one step back234...Document client block-usage and close() (#487)Florimond Mancabefore adoption074cd25
line 235why, one step back235>>> proxy = "..." # Shortcut for {'all': '...'}Drop sync (#544)Tom Christiebefore adoption206c537
line 236why, one step back236>>> async with httpx.Client(proxies=proxy) as client:Document client block-usage and close() (#487)Florimond Mancabefore adoption074cd25
line 237why, one step back237... ...Configure Proxy from Client (#353)Seth Michael Larsonbefore adoptionb0fa085
lines 238–255why, one step back238```239240!!! warning241 To make sure that proxies cannot read your traffic,242 and even if the proxy_url uses HTTPS, it is recommended to243 use HTTPS and tunnel requests if possible.244245By default `HTTPProxy` will operate as a forwarding proxy for `http://...` requests246and will establish a `CONNECT` TCP tunnel for `https://` requests. This doesn't change247regardless of the `proxy_url` being `http` or `https`.248249Proxies can be configured to have different behavior such as forwarding or tunneling all requests:250251```python252proxy = httpx.HTTPProxy(253 proxy_url="https://127.0.0.1",254 proxy_mode=httpx.HTTPProxyMode.TUNNEL_ONLY255)Drop sync (#544)Tom Christiebefore adoption206c537
line 256why, one step back256async with httpx.Client(proxies=proxy) as client:Document client block-usage and close() (#487)Florimond Mancabefore adoption074cd25
line 257why, one step back257 # This request will be tunneled instead of forwarded.Drop sync (#544)Tom Christiebefore adoption206c537
line 258why, one step back258 r = await client.get("http://example.com")Configure Proxy from Client (#353)Seth Michael Larsonbefore adoptionb0fa085
line 259why, one step back259```Add timeout fine-tunning advanced docs section (#476)Mateusz Wośbefore adoption2984499
line 260why, one step back260Drop proxies parameter from the high level API (#485)Yeray Diaz Diazbefore adoption09db6ec
lines 261–265why, one step back261!!! note262263 Per request proxy configuration, i.e. `client.get(url, proxies=...)`,264 has not been implemented yet. To use proxies you must pass the proxy265 information at `Client` initialization.Add timeout fine-tunning advanced docs section (#476)Mateusz Wośbefore adoption2984499
lines 266–268why, one step back266267## Timeout fine-tuning268Multipart files tweaks (#482)Yeray Diaz Diazbefore adoption9ec2cfc
lines 269–270why, one step back269HTTPX offers various request timeout management options. Three types of timeouts270are available: **connect** timeouts, **write** timeouts and **read** timeouts.Add timeout fine-tunning advanced docs section (#476)Mateusz Wośbefore adoption2984499
line 271why, one step back271Multipart files tweaks (#482)Yeray Diaz Diazbefore adoption9ec2cfc
lines 272–281why, one step back272* The **connect timeout** specifies the maximum amount of time to wait until273a connection to the requested host is established. If HTTPX is unable to connect274within this time frame, a `ConnectTimeout` exception is raised.275* The **write timeout** specifies the maximum duration to wait for a chunk of276data to be sent (for example, a chunk of the request body). If HTTPX is unable277to send data within this time frame, a `WriteTimeout` exception is raised.278* The **read timeout** specifies the maximum duration to wait for a chunk of279data to be received (for example, a chunk of the response body). If HTTPX is280unable to receive data within this time frame, a `ReadTimeout` exception is raised.281Add timeout fine-tunning advanced docs section (#476)Mateusz Wośbefore adoption2984499
line 282why, one step back282### Setting timeoutsMultipart files tweaks (#482)Yeray Diaz Diazbefore adoption9ec2cfc
line 283why, one step back283Add timeout fine-tunning advanced docs section (#476)Mateusz Wośbefore adoption2984499
lines 284–289why, one step back284You can set timeouts on two levels:285286- For a given request:287288```python289# Using top-level APIDrop sync (#544)Tom Christiebefore adoption206c537
line 290why, one step back290await httpx.get('http://example.com/api/v1/example', timeout=5)Add timeout fine-tunning advanced docs section (#476)Mateusz Wośbefore adoption2984499
lines 291–292why, one step back291292# Or, with a client:Drop sync (#544)Tom Christiebefore adoption206c537
lines 293–294why, one step back293async with httpx.Client() as client:294 await client.get("http://example.com/api/v1/example", timeout=5)Add timeout fine-tunning advanced docs section (#476)Mateusz Wośbefore adoption2984499
lines 295–299why, one step back295```296297- On a client instance, which results in the given `timeout` being used as a default for requests made with this client:298299```pythonDrop sync (#544)Tom Christiebefore adoption206c537
lines 300–301why, one step back300async with httpx.Client(timeout=5) as client:301 await client.get('http://example.com/api/v1/example')Add timeout fine-tunning advanced docs section (#476)Mateusz Wośbefore adoption2984499
lines 302–305why, one step back302```303304Besides, you can pass timeouts in two forms:305Multipart files tweaks (#482)Yeray Diaz Diazbefore adoption9ec2cfc
line 306why, one step back306- A number, which sets the read, write and connect timeouts to the same value, as in the examples above.Add timeout fine-tunning advanced docs section (#476)Mateusz Wośbefore adoption2984499
lines 307–310why, one step back307- A `TimeoutConfig` instance, which allows to define the read, write and connect timeouts independently:308309```python310timeout = httpx.TimeoutConfig(Multipart files tweaks (#482)Yeray Diaz Diazbefore adoption9ec2cfc
lines 311–312why, one step back311 connect_timeout=5,312 read_timeout=10,Add timeout fine-tunning advanced docs section (#476)Mateusz Wośbefore adoption2984499
lines 313–315why, one step back313 write_timeout=15314)315Drop sync (#544)Tom Christiebefore adoption206c537
line 316why, one step back316resp = await httpx.get('http://example.com/api/v1/example', timeout=timeout)Add timeout fine-tunning advanced docs section (#476)Mateusz Wośbefore adoption2984499
lines 317–319why, one step back317```318319### Default timeoutsMultipart files tweaks (#482)Yeray Diaz Diazbefore adoption9ec2cfc
line 320why, one step back320Add timeout fine-tunning advanced docs section (#476)Mateusz Wośbefore adoption2984499
line 321why, one step back321By default all types of timeouts are set to 5 second.Multipart files tweaks (#482)Yeray Diaz Diazbefore adoption9ec2cfc
line 322why, one step back322Add timeout fine-tunning advanced docs section (#476)Mateusz Wośbefore adoption2984499
line 323why, one step back323### Disabling timeoutsMultipart files tweaks (#482)Yeray Diaz Diazbefore adoption9ec2cfc
lines 324–325why, one step back324325To disable timeouts, you can pass `None` as a timeout parameter.Add timeout fine-tunning advanced docs section (#476)Mateusz Wośbefore adoption2984499
lines 326–330why, one step back326Note that currently this is not supported by the top-level API.327328```python329url = "http://example.com/api/v1/delay/10"330Drop sync (#544)Tom Christiebefore adoption206c537
line 331why, one step back331await httpx.get(url, timeout=None) # Times out after 5sAdd timeout fine-tunning advanced docs section (#476)Mateusz Wośbefore adoption2984499
lines 332–333why, one step back332333Drop sync (#544)Tom Christiebefore adoption206c537
lines 334–335why, one step back334async with httpx.Client(timeout=None) as client:335 await client.get(url) # Does not timeout, returns after 10sAdd timeout fine-tunning advanced docs section (#476)Mateusz Wośbefore adoption2984499
lines 336–338why, one step back336337338timeout = httpx.TimeoutConfig(Multipart files tweaks (#482)Yeray Diaz Diazbefore adoption9ec2cfc
lines 339–340why, one step back339 connect_timeout=5,340 read_timeout=None,Add timeout fine-tunning advanced docs section (#476)Mateusz Wośbefore adoption2984499
lines 341–342why, one step back341 write_timeout=5342)Drop sync (#544)Tom Christiebefore adoption206c537
line 343why, one step back343await httpx.get(url, timeout=timeout) # Does not timeout, returns after 10sMultipart files tweaks (#482)Yeray Diaz Diazbefore adoption9ec2cfc
lines 344–353why, one step back344```345346## Multipart file encoding347348As mentioned in the [quickstart](/quickstart#sending-multipart-file-uploads)349multipart file encoding is available by passing a dictionary with the350name of the payloads as keys and a tuple of elements as values.351352```python353>>> files = {'upload-file': ('report.xls', open('report.xls', 'rb'), 'application/vnd.ms-excel')}Drop sync (#544)Tom Christiebefore adoption206c537
line 354why, one step back354>>> r = await httpx.post("https://httpbin.org/post", files=files)Multipart files tweaks (#482)Yeray Diaz Diazbefore adoption9ec2cfc
lines 355–377why, one step back355>>> print(r.text)356{357 ...358 "files": {359 "upload-file": "<... binary content ...>"360 },361 ...362}363```364365More specifically, this tuple must have at least two elements and maximum of three:366- The first one is an optional file name which can be set to `None`.367- The second may be a file-like object or a string which will be automatically368encoded in UTF-8.369- An optional third element can be included with the370[MIME type](https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/MIME_Types)371of the file being uploaded. If not specified HTTPX will attempt to guess the MIME type372based on the file name specified as the first element or the tuple, if that373is set to `None` or it cannot be inferred from it, HTTPX will default to374`applicaction/octet-stream`.375376```python377>>> files = {'upload-file': (None, 'text content', 'text/plain')}Drop sync (#544)Tom Christiebefore adoption206c537
line 378why, one step back378>>> r = await httpx.post("https://httpbin.org/post", files=files)Multipart files tweaks (#482)Yeray Diaz Diazbefore adoption9ec2cfc
lines 379–387why, one step back379>>> print(r.text)380{381 ...382 "files": {},383 "form": {384 "upload-file": "text-content"385 },386 ...387}Add timeout fine-tunning advanced docs section (#476)Mateusz Wośbefore adoption2984499
line 388why, one step back388```Add docs on SSL certificates (#510)Florimond Mancabefore adoption95b2b24
lines 389–404why, one step back389390## SSL certificates391392When 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).393394### Default CA bundle395396By 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.397398### Using a custom CA bundle399400If 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:401402```python403import httpx404Drop sync (#544)Tom Christiebefore adoption206c537
line 405why, one step back405r = await httpx.get("https://example.org", verify="path/to/client.pem")Add docs on SSL certificates (#510)Florimond Mancabefore adoption95b2b24
lines 406–419why, one step back406```407408### Making HTTPS requests to a local server409410When making requests to local servers, such as a development server running on `localhost`, you will typically be using unencrypted HTTP connections.411412If 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:4134141. Use [trustme-cli](https://github.com/sethmlarson/trustme-cli/) to generate a pair of server key/cert files, and a client cert file.4151. 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.)4161. Tell HTTPX to use the certificates stored in `client.pem`:417418```python419>>> import httpxDrop sync (#544)Tom Christiebefore adoption206c537
line 420why, one step back420>>> r = await httpx.get("https://localhost:8000", verify="/tmp/client.pem")Add docs on SSL certificates (#510)Florimond Mancabefore adoption95b2b24
lines 421–423why, one step back421>>> r422Response <200 OK>423```Drop sync (#544)Tom Christiebefore adoption206c537
lines 424–428why, one step back424425## Support async environments426427### [asyncio](https://docs.python.org/3/library/asyncio.html) (Default)428Drop erronous references to AsyncClient (#559)Tom Christiebefore adoption296c9b4
line 429why, one step back429By default, `Client` uses `asyncio` to perform asynchronous operations and I/O calls.Drop sync (#544)Tom Christiebefore adoption206c537
lines 430–432why, one step back430431### [trio](https://github.com/python-trio/trio)432Drop erronous references to AsyncClient (#559)Tom Christiebefore adoption296c9b4
line 433why, one step back433To make asynchronous requests in `trio` programs, pass a `TrioBackend` to the `Client`:Drop sync (#544)Tom Christiebefore adoption206c537
lines 434–440why, one step back434435```python436import trio437import httpx438from httpx.concurrency.trio import TrioBackend439440async def main():Drop erronous references to AsyncClient (#559)Tom Christiebefore adoption296c9b4
line 441why, one step back441 async with httpx.Client(backend=TrioBackend()) as client:Drop sync (#544)Tom Christiebefore adoption206c537
lines 442–448why, one step back442 ...443444trio.run(main)445```446447!!! important448 `trio` must be installed to import and use the `TrioBackend`.