advanced.md
This file
Path
docs/advanced.md
445 lines · 13.8 KiB · Markdown
Last changed by
Drop erronous references to AsyncClient (#559)Tom Christiebefore adoption296c9b4
History of this fileTake it
The walk stopped after 200 commits: lines older than 7511d7a point at it.
Update README.mdTom Christiebefore adoption7511d7a
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:15Update README.mdTom Christiebefore adoption7511d7a
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```pythonUpdate README.mdTom Christiebefore adoption7511d7a
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...Update README.mdTom Christiebefore adoption7511d7a
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).74Update README.mdTom Christiebefore adoption7511d7a
lines 75–77why, one step back75## Calling into Python Web Apps7677You can configure an `httpx` client to call directly into a Python webDrop sync (#544)Tom Christiebefore adoption206c537
line 78why, one step back78application using either the ASGI protocol.Update README.mdTom Christiebefore adoption7511d7a
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.Update README.mdTom Christiebefore adoption7511d7a
line 84why, one step back84Drop sync (#544)Tom Christiebefore adoption206c537
line 85why, one step back85Let's take this Starlette application as an example:Update README.mdTom Christiebefore adoption7511d7a
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 RouteUpdate README.mdTom Christiebefore adoption7511d7a
lines 91–92why, one step back9192Drop sync (#544)Tom Christiebefore adoption206c537
lines 93–94why, one step back93async def hello():94 return HTMLResponse("Hello World!")Update README.mdTom Christiebefore adoption7511d7a
lines 95–96why, one step back9596Drop sync (#544)Tom Christiebefore adoption206c537
line 97why, one step back97app = Starlette(routes=[Route("/", hello)])Update README.mdTom Christiebefore adoption7511d7a
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```109Fix some grammar in Advanced docs (#461)thebigmunchbefore adoption3917866
line 110why, one step back110For some more complex cases you might need to customize the WSGI or ASGIDrop `raise_app_exceptions` Client argument. (#238)Tom Christiebefore adoptionef9fc0f
lines 111–112why, one step back111dispatch. This allows you to:112Fix some grammar in Advanced docs (#461)thebigmunchbefore adoption3917866
line 113why, one step back113* Inspect 500 error responses rather than raise exceptions by setting `raise_app_exceptions=False`.Drop sync (#544)Tom Christiebefore adoption206c537
lines 114–115why, one step back114* Mount the WSGI or ASGI application at a subpath by setting `root_path`.115* Use a given client address for requests by setting `client`.Drop `raise_app_exceptions` Client argument. (#238)Tom Christiebefore adoptionef9fc0f
lines 116–120why, one step back116117For example:118119```python120# Instantiate a client that makes WSGI requests with a client IP of "1.2.3.4".Drop sync (#544)Tom Christiebefore adoption206c537
lines 121–122why, one step back121dispatch = httpx.dispatch.ASGIDispatch(app=app, remote_addr="1.2.3.4")122async 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
lines 124–125why, one step back124```125Add `build_request` to `Client` (#319)Dobrovolsky Bogdanbefore adoption08edfac
lines 126–131why, one step back126## Build Request127128You can use `Client.build_request()` to build a request and129make modifications before sending the request.130131```pythonDrop sync (#544)Tom Christiebefore adoption206c537
line 132why, one step back132>>> async with httpx.Client() as client:Document client block-usage and close() (#487)Florimond Mancabefore adoption074cd25
lines 133–134why, one step back133... req = client.build_request("OPTIONS", "https://example.com")134... req.url.full_path = "*" # Build an 'OPTIONS *' request for CORSDrop sync (#544)Tom Christiebefore adoption206c537
line 135why, one step back135... r = await client.send(req)Document client block-usage and close() (#487)Florimond Mancabefore adoption074cd25
lines 136–137why, one step back136...137>>> rAdd `build_request` to `Client` (#319)Dobrovolsky Bogdanbefore adoption08edfac
lines 138–140why, one step back138<Response [200 OK]>139```140Fix typos, spelling issues, and grammar in docs (#426)Kyle Galbraithbefore adoptionb65bce5
line 141why, one step back141## Specify the version of the HTTP protocolAdd example of specifying HTTP versions (#337)Paul (xobb) Chubatyybefore adoption0c4460e
line 142why, one step back142Fix typos, spelling issues, and grammar in docs (#426)Kyle Galbraithbefore adoptionb65bce5
line 143why, one step back143One 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 144–147why, one step back144145For example:146147```pythonDrop sync (#544)Tom Christiebefore adoption206c537
lines 148–149why, one step back148async with httpx.Client(http_versions=["HTTP/1.1"]) as h11_client:149 h11_response = await h11_client.get("https://myserver.com")Add example of specifying HTTP versions (#337)Paul (xobb) Chubatyybefore adoption0c4460e
line 150why, one step back150Drop sync (#544)Tom Christiebefore adoption206c537
lines 151–152why, one step back151async with httpx.Client(http_versions=["HTTP/2"]) as h2_client:152 h2_response = await h2_client.get("https://myserver.com")Add example of specifying HTTP versions (#337)Paul (xobb) Chubatyybefore adoption0c4460e
lines 153–154why, one step back153```154Documented netrc support (#225)Can Sarıgölbefore adoption21939fd
lines 155–159why, one step back155## .netrc Support156157HTTPX supports .netrc file. In `trust_env=True` cases, if auth parameter is158not defined, HTTPX tries to add auth into request's header from .netrc file.159Cache netrc authentication per-client (#400)Can Sarıgölbefore adoption84731c8
lines 160–164why, one step back160!!! note161 The NETRC file is cached across requests made by a client.162 If you need to refresh the cache (e.g. because the NETRC file has changed),163 you should create a new client or restart the interpreter.164Documented netrc support (#225)Can Sarıgölbefore adoption21939fd
lines 165–166why, one step back165As default `trust_env` is true. To set false:166```pythonDrop sync (#544)Tom Christiebefore adoption206c537
line 167why, one step back167>>> await httpx.get('https://example.org/', trust_env=False)Documented netrc support (#225)Can Sarıgölbefore adoption21939fd
lines 168–186why, one step back168```169170If `NETRC` environment is empty, HTTPX tries to use default files.171(`~/.netrc`, `~/_netrc`)172173To change `NETRC` environment:174```python175>>> import os176>>> os.environ["NETRC"] = "my_default_folder/.my_netrc"177```178179.netrc file content example:180```181machine netrcexample.org182login example-username183password example-password184185...186```Configure Proxy from Client (#353)Seth Michael Larsonbefore adoptionb0fa085
line 187why, one step back187Add Unix Domain Sockets section to advanced docs page (#542)Jonas Lundbergbefore adoptionf0e6acb
lines 188–197why, one step back188## Unix Domain Sockets189190You can configure an `httpx` client to connect through a unix domain socket via the `uds` parameter. This is useful when making requests to a server that is bound to a socket file rather than an IP address.191192Here's an example requesting the Docker Engine API:193194```python195import httpx196197Drop sync (#544)Tom Christiebefore adoption206c537
line 198why, one step back198async with httpx.Client(uds="/var/run/docker.sock") as client:Add Unix Domain Sockets section to advanced docs page (#542)Jonas Lundbergbefore adoptionf0e6acb
line 199why, one step back199 # This request will connect through the socket file.Drop sync (#544)Tom Christiebefore adoption206c537
line 200why, one step back200 resp = await client.get("http://localhost/version")Add Unix Domain Sockets section to advanced docs page (#542)Jonas Lundbergbefore adoptionf0e6acb
lines 201–202why, one step back201```202Configure Proxy from Client (#353)Seth Michael Larsonbefore adoptionb0fa085
lines 203–209why, one step back203## HTTP Proxying204205HTTPX supports setting up proxies the same way that Requests does via the `proxies` parameter.206For example to forward all HTTP traffic to `http://127.0.0.1:3080` and all HTTPS traffic207to `http://127.0.0.1:3081` your `proxies` config would look like this:208209```pythonDocument client block-usage and close() (#487)Florimond Mancabefore adoption074cd25
lines 210–213why, one step back210>>> proxies = {211... "http": "http://127.0.0.1:3080",212... "https": "http://127.0.0.1:3081"213... }Drop sync (#544)Tom Christiebefore adoption206c537
line 214why, one step back214>>> async with httpx.Client(proxies=proxies) as client:Document client block-usage and close() (#487)Florimond Mancabefore adoption074cd25
line 215why, one step back215... ...Configure Proxy from Client (#353)Seth Michael Larsonbefore adoptionb0fa085
lines 216–222why, one step back216```217218Proxies can be configured for a specific scheme and host, all schemes of a host,219all hosts for a scheme, or for all requests. When determining which proxy configuration220to use for a given request this same order is used.221222```pythonDocument client block-usage and close() (#487)Florimond Mancabefore adoption074cd25
lines 223–228why, one step back223>>> proxies = {224... "http://example.com": "...", # Host+Scheme225... "all://example.com": "...", # Host226... "http": "...", # Scheme227... "all": "...", # All228... }Drop sync (#544)Tom Christiebefore adoption206c537
line 229why, one step back229>>> async with httpx.Client(proxies=proxies) as client:Document client block-usage and close() (#487)Florimond Mancabefore adoption074cd25
line 230why, one step back230... ...Drop sync (#544)Tom Christiebefore adoption206c537
line 231why, one step back231...Document client block-usage and close() (#487)Florimond Mancabefore adoption074cd25
line 232why, one step back232>>> proxy = "..." # Shortcut for {'all': '...'}Drop sync (#544)Tom Christiebefore adoption206c537
line 233why, one step back233>>> async with httpx.Client(proxies=proxy) as client:Document client block-usage and close() (#487)Florimond Mancabefore adoption074cd25
line 234why, one step back234... ...Configure Proxy from Client (#353)Seth Michael Larsonbefore adoptionb0fa085
lines 235–252why, one step back235```236237!!! warning238 To make sure that proxies cannot read your traffic,239 and even if the proxy_url uses HTTPS, it is recommended to240 use HTTPS and tunnel requests if possible.241242By default `HTTPProxy` will operate as a forwarding proxy for `http://...` requests243and will establish a `CONNECT` TCP tunnel for `https://` requests. This doesn't change244regardless of the `proxy_url` being `http` or `https`.245246Proxies can be configured to have different behavior such as forwarding or tunneling all requests:247248```python249proxy = httpx.HTTPProxy(250 proxy_url="https://127.0.0.1",251 proxy_mode=httpx.HTTPProxyMode.TUNNEL_ONLY252)Drop sync (#544)Tom Christiebefore adoption206c537
line 253why, one step back253async with httpx.Client(proxies=proxy) as client:Document client block-usage and close() (#487)Florimond Mancabefore adoption074cd25
line 254why, one step back254 # This request will be tunneled instead of forwarded.Drop sync (#544)Tom Christiebefore adoption206c537
line 255why, one step back255 r = await client.get("http://example.com")Configure Proxy from Client (#353)Seth Michael Larsonbefore adoptionb0fa085
line 256why, one step back256```Add timeout fine-tunning advanced docs section (#476)Mateusz Wośbefore adoption2984499
line 257why, one step back257Drop proxies parameter from the high level API (#485)Yeray Diaz Diazbefore adoption09db6ec
lines 258–262why, one step back258!!! note259260 Per request proxy configuration, i.e. `client.get(url, proxies=...)`,261 has not been implemented yet. To use proxies you must pass the proxy262 information at `Client` initialization.Add timeout fine-tunning advanced docs section (#476)Mateusz Wośbefore adoption2984499
lines 263–265why, one step back263264## Timeout fine-tuning265Multipart files tweaks (#482)Yeray Diaz Diazbefore adoption9ec2cfc
lines 266–267why, one step back266HTTPX offers various request timeout management options. Three types of timeouts267are available: **connect** timeouts, **write** timeouts and **read** timeouts.Add timeout fine-tunning advanced docs section (#476)Mateusz Wośbefore adoption2984499
line 268why, one step back268Multipart files tweaks (#482)Yeray Diaz Diazbefore adoption9ec2cfc
lines 269–278why, one step back269* The **connect timeout** specifies the maximum amount of time to wait until270a connection to the requested host is established. If HTTPX is unable to connect271within this time frame, a `ConnectTimeout` exception is raised.272* The **write timeout** specifies the maximum duration to wait for a chunk of273data to be sent (for example, a chunk of the request body). If HTTPX is unable274to send data within this time frame, a `WriteTimeout` exception is raised.275* The **read timeout** specifies the maximum duration to wait for a chunk of276data to be received (for example, a chunk of the response body). If HTTPX is277unable to receive data within this time frame, a `ReadTimeout` exception is raised.278Add timeout fine-tunning advanced docs section (#476)Mateusz Wośbefore adoption2984499
line 279why, one step back279### Setting timeoutsMultipart files tweaks (#482)Yeray Diaz Diazbefore adoption9ec2cfc
line 280why, one step back280Add timeout fine-tunning advanced docs section (#476)Mateusz Wośbefore adoption2984499
lines 281–286why, one step back281You can set timeouts on two levels:282283- For a given request:284285```python286# Using top-level APIDrop sync (#544)Tom Christiebefore adoption206c537
line 287why, one step back287await httpx.get('http://example.com/api/v1/example', timeout=5)Add timeout fine-tunning advanced docs section (#476)Mateusz Wośbefore adoption2984499
lines 288–289why, one step back288289# Or, with a client:Drop sync (#544)Tom Christiebefore adoption206c537
lines 290–291why, one step back290async with httpx.Client() as client:291 await client.get("http://example.com/api/v1/example", timeout=5)Add timeout fine-tunning advanced docs section (#476)Mateusz Wośbefore adoption2984499
lines 292–296why, one step back292```293294- On a client instance, which results in the given `timeout` being used as a default for requests made with this client:295296```pythonDrop sync (#544)Tom Christiebefore adoption206c537
lines 297–298why, one step back297async with httpx.Client(timeout=5) as client:298 await client.get('http://example.com/api/v1/example')Add timeout fine-tunning advanced docs section (#476)Mateusz Wośbefore adoption2984499
lines 299–302why, one step back299```300301Besides, you can pass timeouts in two forms:302Multipart files tweaks (#482)Yeray Diaz Diazbefore adoption9ec2cfc
line 303why, one step back303- 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 304–307why, one step back304- A `TimeoutConfig` instance, which allows to define the read, write and connect timeouts independently:305306```python307timeout = httpx.TimeoutConfig(Multipart files tweaks (#482)Yeray Diaz Diazbefore adoption9ec2cfc
lines 308–309why, one step back308 connect_timeout=5,309 read_timeout=10,Add timeout fine-tunning advanced docs section (#476)Mateusz Wośbefore adoption2984499
lines 310–312why, one step back310 write_timeout=15311)312Drop sync (#544)Tom Christiebefore adoption206c537
line 313why, one step back313resp = await httpx.get('http://example.com/api/v1/example', timeout=timeout)Add timeout fine-tunning advanced docs section (#476)Mateusz Wośbefore adoption2984499
lines 314–316why, one step back314```315316### Default timeoutsMultipart files tweaks (#482)Yeray Diaz Diazbefore adoption9ec2cfc
line 317why, one step back317Add timeout fine-tunning advanced docs section (#476)Mateusz Wośbefore adoption2984499
line 318why, one step back318By default all types of timeouts are set to 5 second.Multipart files tweaks (#482)Yeray Diaz Diazbefore adoption9ec2cfc
line 319why, one step back319Add timeout fine-tunning advanced docs section (#476)Mateusz Wośbefore adoption2984499
line 320why, one step back320### Disabling timeoutsMultipart files tweaks (#482)Yeray Diaz Diazbefore adoption9ec2cfc
lines 321–322why, one step back321322To disable timeouts, you can pass `None` as a timeout parameter.Add timeout fine-tunning advanced docs section (#476)Mateusz Wośbefore adoption2984499
lines 323–327why, one step back323Note that currently this is not supported by the top-level API.324325```python326url = "http://example.com/api/v1/delay/10"327Drop sync (#544)Tom Christiebefore adoption206c537
line 328why, one step back328await httpx.get(url, timeout=None) # Times out after 5sAdd timeout fine-tunning advanced docs section (#476)Mateusz Wośbefore adoption2984499
lines 329–330why, one step back329330Drop sync (#544)Tom Christiebefore adoption206c537
lines 331–332why, one step back331async with httpx.Client(timeout=None) as client:332 await client.get(url) # Does not timeout, returns after 10sAdd timeout fine-tunning advanced docs section (#476)Mateusz Wośbefore adoption2984499
lines 333–335why, one step back333334335timeout = httpx.TimeoutConfig(Multipart files tweaks (#482)Yeray Diaz Diazbefore adoption9ec2cfc
lines 336–337why, one step back336 connect_timeout=5,337 read_timeout=None,Add timeout fine-tunning advanced docs section (#476)Mateusz Wośbefore adoption2984499
lines 338–339why, one step back338 write_timeout=5339)Drop sync (#544)Tom Christiebefore adoption206c537
line 340why, one step back340await httpx.get(url, timeout=timeout) # Does not timeout, returns after 10sMultipart files tweaks (#482)Yeray Diaz Diazbefore adoption9ec2cfc
lines 341–350why, one step back341```342343## Multipart file encoding344345As mentioned in the [quickstart](/quickstart#sending-multipart-file-uploads)346multipart file encoding is available by passing a dictionary with the347name of the payloads as keys and a tuple of elements as values.348349```python350>>> files = {'upload-file': ('report.xls', open('report.xls', 'rb'), 'application/vnd.ms-excel')}Drop sync (#544)Tom Christiebefore adoption206c537
line 351why, one step back351>>> r = await httpx.post("https://httpbin.org/post", files=files)Multipart files tweaks (#482)Yeray Diaz Diazbefore adoption9ec2cfc
lines 352–374why, one step back352>>> print(r.text)353{354 ...355 "files": {356 "upload-file": "<... binary content ...>"357 },358 ...359}360```361362More specifically, this tuple must have at least two elements and maximum of three:363- The first one is an optional file name which can be set to `None`.364- The second may be a file-like object or a string which will be automatically365encoded in UTF-8.366- An optional third element can be included with the367[MIME type](https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/MIME_Types)368of the file being uploaded. If not specified HTTPX will attempt to guess the MIME type369based on the file name specified as the first element or the tuple, if that370is set to `None` or it cannot be inferred from it, HTTPX will default to371`applicaction/octet-stream`.372373```python374>>> files = {'upload-file': (None, 'text content', 'text/plain')}Drop sync (#544)Tom Christiebefore adoption206c537
line 375why, one step back375>>> r = await httpx.post("https://httpbin.org/post", files=files)Multipart files tweaks (#482)Yeray Diaz Diazbefore adoption9ec2cfc
lines 376–384why, one step back376>>> print(r.text)377{378 ...379 "files": {},380 "form": {381 "upload-file": "text-content"382 },383 ...384}Add timeout fine-tunning advanced docs section (#476)Mateusz Wośbefore adoption2984499
line 385why, one step back385```Add docs on SSL certificates (#510)Florimond Mancabefore adoption95b2b24
lines 386–401why, one step back386387## SSL certificates388389When 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).390391### Default CA bundle392393By 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.394395### Using a custom CA bundle396397If 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:398399```python400import httpx401Drop sync (#544)Tom Christiebefore adoption206c537
line 402why, one step back402r = await httpx.get("https://example.org", verify="path/to/client.pem")Add docs on SSL certificates (#510)Florimond Mancabefore adoption95b2b24
lines 403–416why, one step back403```404405### Making HTTPS requests to a local server406407When making requests to local servers, such as a development server running on `localhost`, you will typically be using unencrypted HTTP connections.408409If 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:4104111. Use [trustme-cli](https://github.com/sethmlarson/trustme-cli/) to generate a pair of server key/cert files, and a client cert file.4121. 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.)4131. Tell HTTPX to use the certificates stored in `client.pem`:414415```python416>>> import httpxDrop sync (#544)Tom Christiebefore adoption206c537
line 417why, one step back417>>> r = await httpx.get("https://localhost:8000", verify="/tmp/client.pem")Add docs on SSL certificates (#510)Florimond Mancabefore adoption95b2b24
lines 418–420why, one step back418>>> r419Response <200 OK>420```Drop sync (#544)Tom Christiebefore adoption206c537
lines 421–425why, one step back421422## Support async environments423424### [asyncio](https://docs.python.org/3/library/asyncio.html) (Default)425Drop erronous references to AsyncClient (#559)Tom Christiebefore adoption296c9b4
line 426why, one step back426By default, `Client` uses `asyncio` to perform asynchronous operations and I/O calls.Drop sync (#544)Tom Christiebefore adoption206c537
lines 427–429why, one step back427428### [trio](https://github.com/python-trio/trio)429Drop erronous references to AsyncClient (#559)Tom Christiebefore adoption296c9b4
line 430why, one step back430To make asynchronous requests in `trio` programs, pass a `TrioBackend` to the `Client`:Drop sync (#544)Tom Christiebefore adoption206c537
lines 431–437why, one step back431432```python433import trio434import httpx435from httpx.concurrency.trio import TrioBackend436437async def main():Drop erronous references to AsyncClient (#559)Tom Christiebefore adoption296c9b4
line 438why, one step back438 async with httpx.Client(backend=TrioBackend()) as client:Drop sync (#544)Tom Christiebefore adoption206c537
lines 439–445why, one step back439 ...440441trio.run(main)442```443444!!! important445 `trio` must be installed to import and use the `TrioBackend`.