advanced.md
This file
Path
docs/advanced.md
458 lines · 14.8 KiB · Markdown
Last changed by
Improve backend docs, particularly wrt. autodetection (#595)Tom Christiebefore adoptionf8794cb
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```143Documented netrc support (#225)Can Sarıgölbefore adoption21939fd
lines 144–148why, one step back144## .netrc Support145146HTTPX 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.148Cache netrc authentication per-client (#400)Can Sarıgölbefore adoption84731c8
lines 149–153why, one step back149!!! note150 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.153Documented netrc support (#225)Can Sarıgölbefore adoption21939fd
lines 154–155why, one step back154As default `trust_env` is true. To set false:155```pythonDrop sync (#544)Tom Christiebefore adoption206c537
line 156why, one step back156>>> await httpx.get('https://example.org/', trust_env=False)Documented netrc support (#225)Can Sarıgölbefore adoption21939fd
lines 157–175why, one step back157```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 back176Add Unix Domain Sockets section to advanced docs page (#542)Jonas Lundbergbefore adoptionf0e6acb
lines 177–186why, one step back177## Unix Domain Sockets178179You 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 httpx185186Drop sync (#544)Tom Christiebefore adoption206c537
line 187why, one step back187async 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 back188 # This request will connect through the socket file.Drop sync (#544)Tom Christiebefore adoption206c537
line 189why, one step back189 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 back190```191Configure Proxy from Client (#353)Seth Michael Larsonbefore adoptionb0fa085
lines 192–198why, one step back192## HTTP Proxying193194HTTPX 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```pythonDocument client block-usage and close() (#487)Florimond Mancabefore adoption074cd25
lines 199–202why, one step back199>>> 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 back203>>> async with httpx.Client(proxies=proxies) as client:Document client block-usage and close() (#487)Florimond Mancabefore adoption074cd25
line 204why, one step back204... ...Configure Proxy from Client (#353)Seth Michael Larsonbefore adoptionb0fa085
lines 205–211why, one step back205```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```pythonDocument client block-usage and close() (#487)Florimond Mancabefore adoption074cd25
lines 212–217why, one step back212>>> 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 back218>>> async with httpx.Client(proxies=proxies) as client:Document client block-usage and close() (#487)Florimond Mancabefore adoption074cd25
line 219why, one step back219... ...Drop sync (#544)Tom Christiebefore adoption206c537
line 220why, one step back220...Document client block-usage and close() (#487)Florimond Mancabefore adoption074cd25
line 221why, one step back221>>> proxy = "..." # Shortcut for {'all': '...'}Drop sync (#544)Tom Christiebefore adoption206c537
line 222why, one step back222>>> async with httpx.Client(proxies=proxy) as client:Document client block-usage and close() (#487)Florimond Mancabefore adoption074cd25
line 223why, one step back223... ...Configure Proxy from Client (#353)Seth Michael Larsonbefore adoptionb0fa085
lines 224–241why, one step back224```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 back242async with httpx.Client(proxies=proxy) as client:Document client block-usage and close() (#487)Florimond Mancabefore adoption074cd25
line 243why, one step back243 # This request will be tunneled instead of forwarded.Drop sync (#544)Tom Christiebefore adoption206c537
line 244why, one step back244 r = await client.get("http://example.com")Configure Proxy from Client (#353)Seth Michael Larsonbefore adoptionb0fa085
line 245why, one step back245```Add timeout fine-tunning advanced docs section (#476)Mateusz Wośbefore adoption2984499
line 246why, one step back246Drop proxies parameter from the high level API (#485)Yeray Diaz Diazbefore adoption09db6ec
lines 247–251why, one step back247!!! note248249 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
line 252why, one step back252Allow default+override timeout style (#593)Tom Christiebefore adoption2f54b20
line 253why, one step back253## Timeout ConfigurationAdd timeout fine-tunning advanced docs section (#476)Mateusz Wośbefore adoption2984499
line 254why, one step back254Allow default+override timeout style (#593)Tom Christiebefore adoption2f54b20
line 255why, one step back255HTTPX is careful to enforce timeouts everywhere by default.Add timeout fine-tunning advanced docs section (#476)Mateusz Wośbefore adoption2984499
line 256why, one step back256Allow default+override timeout style (#593)Tom Christiebefore adoption2f54b20
lines 257–258why, one step back257The default behavior is to raise a `TimeoutException` after 5 seconds of258network inactivity.Multipart files tweaks (#482)Yeray Diaz Diazbefore adoption9ec2cfc
line 259why, one step back259Allow default+override timeout style (#593)Tom Christiebefore adoption2f54b20
line 260why, one step back260### Setting and disabling timeoutsMultipart files tweaks (#482)Yeray Diaz Diazbefore adoption9ec2cfc
line 261why, one step back261Allow default+override timeout style (#593)Tom Christiebefore adoption2f54b20
line 262why, one step back262You can set timeouts for an individual request:Add timeout fine-tunning advanced docs section (#476)Mateusz Wośbefore adoption2984499
lines 263–264why, one step back263264```pythonAllow default+override timeout style (#593)Tom Christiebefore adoption2f54b20
lines 265–266why, one step back265# Using the top-level API:266await httpx.get('http://example.com/api/v1/example', timeout=10.0)Add timeout fine-tunning advanced docs section (#476)Mateusz Wośbefore adoption2984499
line 267why, one step back267Allow default+override timeout style (#593)Tom Christiebefore adoption2f54b20
line 268why, one step back268# Using a client instance:Drop sync (#544)Tom Christiebefore adoption206c537
line 269why, one step back269async with httpx.Client() as client:Allow default+override timeout style (#593)Tom Christiebefore adoption2f54b20
line 270why, one step back270 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 271–272why, one step back271```272Allow default+override timeout style (#593)Tom Christiebefore adoption2f54b20
line 273why, one step back273Or disable timeouts for an individual request:Add timeout fine-tunning advanced docs section (#476)Mateusz Wośbefore adoption2984499
lines 274–275why, one step back274275```pythonAllow default+override timeout style (#593)Tom Christiebefore adoption2f54b20
lines 276–281why, one step back276# Using the top-level API:277await httpx.get('http://example.com/api/v1/example', timeout=None)278279# Using a client instance:280async with httpx.Client() as client:281 await client.get("http://example.com/api/v1/example", timeout=None)Add timeout fine-tunning advanced docs section (#476)Mateusz Wośbefore adoption2984499
lines 282–283why, one step back282```283Allow default+override timeout style (#593)Tom Christiebefore adoption2f54b20
line 284why, one step back284### Setting a default timeout on a clientAdd timeout fine-tunning advanced docs section (#476)Mateusz Wośbefore adoption2984499
line 285why, one step back285Allow default+override timeout style (#593)Tom Christiebefore adoption2f54b20
lines 286–287why, one step back286You can set a timeout on a client instance, which results in the given287`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 288–289why, one step back288289```pythonAllow default+override timeout style (#593)Tom Christiebefore adoption2f54b20
lines 290–292why, one step back290client = httpx.Client() # Use a default 5s timeout everywhere.291client = httpx.Client(timeout=10.0) # Use a default 10s timeout everywhere.292client = httpx.Client(timeout=None) # Disable all timeouts by default.Add timeout fine-tunning advanced docs section (#476)Mateusz Wośbefore adoption2984499
lines 293–294why, one step back293```294Allow default+override timeout style (#593)Tom Christiebefore adoption2f54b20
line 295why, one step back295### Fine tuning the configurationMultipart files tweaks (#482)Yeray Diaz Diazbefore adoption9ec2cfc
line 296why, one step back296Allow default+override timeout style (#593)Tom Christiebefore adoption2f54b20
line 297why, one step back297HTTPX also allows you to specify the timeout behavior in more fine grained detail.Multipart files tweaks (#482)Yeray Diaz Diazbefore adoption9ec2cfc
line 298why, one step back298Allow default+override timeout style (#593)Tom Christiebefore adoption2f54b20
lines 299–300why, one step back299There are four different types of timeouts that may occur. These are **connect**,300**read**, **write**, and **pool** timeouts.Multipart files tweaks (#482)Yeray Diaz Diazbefore adoption9ec2cfc
line 301why, one step back301Allow default+override timeout style (#593)Tom Christiebefore adoption2f54b20
lines 302–315why, one step back302* The **connect timeout** specifies the maximum amount of time to wait until303a connection to the requested host is established. If HTTPX is unable to connect304within this time frame, a `ConnectTimeout` exception is raised.305* The **read timeout** specifies the maximum duration to wait for a chunk of306data to be received (for example, a chunk of the response body). If HTTPX is307unable to receive data within this time frame, a `ReadTimeout` exception is raised.308* The **write timeout** specifies the maximum duration to wait for a chunk of309data to be sent (for example, a chunk of the request body). If HTTPX is unable310to send data within this time frame, a `WriteTimeout` exception is raised.311* The **pool timeout** specifies the maximum duration to wait for acquiring312a connection from the connection pool. If HTTPX is unable to acquire a connection313within this time frame, a `PoolTimeout` exception is raised. A related314configuration here is the maximum number of allowable connections in the315connection pool, which is configured by the `pool_limits`.Add timeout fine-tunning advanced docs section (#476)Mateusz Wośbefore adoption2984499
line 316why, one step back316Allow default+override timeout style (#593)Tom Christiebefore adoption2f54b20
lines 317–318why, one step back317You can configure the timeout behavior for any of these values...318Add timeout fine-tunning advanced docs section (#476)Mateusz Wośbefore adoption2984499
line 319why, one step back319```pythonAllow default+override timeout style (#593)Tom Christiebefore adoption2f54b20
lines 320–322why, one step back320# A client with a 60s timeout for connecting, and a 10s timeout elsewhere.321timeout = httpx.Timeout(10.0, connect_timeout=60.0)322client = httpx.Client(timeout=timeout)Add timeout fine-tunning advanced docs section (#476)Mateusz Wośbefore adoption2984499
line 323why, one step back323Allow default+override timeout style (#593)Tom Christiebefore adoption2f54b20
line 324why, one step back324response = await client.get('http://example.com/')Multipart files tweaks (#482)Yeray Diaz Diazbefore adoption9ec2cfc
lines 325–330why, one step back325```326327## Multipart file encoding328329As mentioned in the [quickstart](/quickstart#sending-multipart-file-uploads)330multipart file encoding is available by passing a dictionary with theClarify multipart documentation (#580)Mattwmaster58before adoption33cb397
line 331why, one step back331name 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 332–334why, one step back332333```python334>>> files = {'upload-file': ('report.xls', open('report.xls', 'rb'), 'application/vnd.ms-excel')}Drop sync (#544)Tom Christiebefore adoption206c537
line 335why, one step back335>>> r = await httpx.post("https://httpbin.org/post", files=files)Multipart files tweaks (#482)Yeray Diaz Diazbefore adoption9ec2cfc
lines 336–345why, one step back336>>> print(r.text)337{338 ...339 "files": {340 "upload-file": "<... binary content ...>"341 },342 ...343}344```345Clarify multipart documentation (#580)Mattwmaster58before adoption33cb397
line 346why, one step back346More specifically, if a tuple is used as a value, it must have between 2 and 3 elements:Update advanced.mdTom Christiebefore adoption871b0b5
line 347why, one step back347Clarify multipart documentation (#580)Mattwmaster58before adoption33cb397
lines 348–349why, one step back348- The first element is an optional file name which can be set to `None`.349- The second element may be a file-like object or a string which will be automaticallyMultipart files tweaks (#482)Yeray Diaz Diazbefore adoption9ec2cfc
line 350why, one step back350encoded in UTF-8.Better HTTP/2 defaults. (#584)Tom Christiebefore adoption30229f1
line 351why, one step back351- An optional third element can be used to specify theMultipart files tweaks (#482)Yeray Diaz Diazbefore adoption9ec2cfc
line 352why, one step back352[MIME type](https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/MIME_Types)Clarify multipart documentation (#580)Mattwmaster58before adoption33cb397
lines 353–356why, one step back353of the file being uploaded. If not specified HTTPX will attempt to guess the MIME type based354on the file name, with unknown file extensions defaulting to "application/octet-stream".355If the file name is explicitly set to `None` then HTTPX will not include a content-type356MIME header field.Multipart files tweaks (#482)Yeray Diaz Diazbefore adoption9ec2cfc
lines 357–359why, one step back357358```python359>>> files = {'upload-file': (None, 'text content', 'text/plain')}Drop sync (#544)Tom Christiebefore adoption206c537
line 360why, one step back360>>> r = await httpx.post("https://httpbin.org/post", files=files)Multipart files tweaks (#482)Yeray Diaz Diazbefore adoption9ec2cfc
lines 361–369why, one step back361>>> print(r.text)362{363 ...364 "files": {},365 "form": {366 "upload-file": "text-content"367 },368 ...369}Add timeout fine-tunning advanced docs section (#476)Mateusz Wośbefore adoption2984499
line 370why, one step back370```Add docs on SSL certificates (#510)Florimond Mancabefore adoption95b2b24
lines 371–386why, one step back371372## SSL certificates373374When 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).375376### Default CA bundle377378By 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.379380### Using a custom CA bundle381382If 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:383384```python385import httpx386Drop sync (#544)Tom Christiebefore adoption206c537
line 387why, one step back387r = await httpx.get("https://example.org", verify="path/to/client.pem")Add docs on SSL certificates (#510)Florimond Mancabefore adoption95b2b24
lines 388–401why, one step back388```389390### Making HTTPS requests to a local server391392When making requests to local servers, such as a development server running on `localhost`, you will typically be using unencrypted HTTP connections.393394If 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:3953961. Use [trustme-cli](https://github.com/sethmlarson/trustme-cli/) to generate a pair of server key/cert files, and a client cert file.3971. 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.)3981. Tell HTTPX to use the certificates stored in `client.pem`:399400```python401>>> import httpxDrop sync (#544)Tom Christiebefore adoption206c537
line 402why, one step back402>>> r = await httpx.get("https://localhost:8000", verify="/tmp/client.pem")Add docs on SSL certificates (#510)Florimond Mancabefore adoption95b2b24
lines 403–405why, one step back403>>> r404Response <200 OK>405```Drop sync (#544)Tom Christiebefore adoption206c537
line 406why, one step back406Improve backend docs, particularly wrt. autodetection (#595)Tom Christiebefore adoptionf8794cb
line 407why, one step back407## Supported async environmentsDrop sync (#544)Tom Christiebefore adoption206c537
line 408why, one step back408Improve backend docs, particularly wrt. autodetection (#595)Tom Christiebefore adoptionf8794cb
line 409why, one step back409HTTPX supports either `asyncio` or `trio` as an async environment.Drop sync (#544)Tom Christiebefore adoption206c537
line 410why, one step back410Improve backend docs, particularly wrt. autodetection (#595)Tom Christiebefore adoptionf8794cb
lines 411–412why, one step back411By default it will auto-detect which of those two to use as the backend412for socket operations and concurrency primitives.Drop sync (#544)Tom Christiebefore adoption206c537
line 413why, one step back413Improve backend docs, particularly wrt. autodetection (#595)Tom Christiebefore adoptionf8794cb
lines 414–415why, one step back414You can also explicitly select a backend by instantiating a client with the415`backend` argument...Drop sync (#544)Tom Christiebefore adoption206c537
line 416why, one step back416Improve backend docs, particularly wrt. autodetection (#595)Tom Christiebefore adoptionf8794cb
lines 417–421why, one step back417```python418client = httpx.Client(backend='auto') # Autodetection. The default case.419client = httpx.Client(backend='asyncio') # Use asyncio as the backend.420client = httpx.Client(backend='trio') # Use trio as the backend.421```Drop sync (#544)Tom Christiebefore adoption206c537
line 422why, one step back422Improve backend docs, particularly wrt. autodetection (#595)Tom Christiebefore adoptionf8794cb
lines 423–427why, one step back423### [AsyncIO](https://docs.python.org/3/library/asyncio.html)424425AsyncIO is Python's [built-in library](https://docs.python.org/3/library/asyncio.html)426for writing concurrent code with the async/await syntax.427Drop sync (#544)Tom Christiebefore adoption206c537
line 428why, one step back428```pythonImprove backend docs, particularly wrt. autodetection (#595)Tom Christiebefore adoptionf8794cb
line 429why, one step back429import asyncioDrop sync (#544)Tom Christiebefore adoption206c537
lines 430–432why, one step back430import httpx431432async def main():Improve backend docs, particularly wrt. autodetection (#595)Tom Christiebefore adoptionf8794cb
lines 433–435why, one step back433 client = httpx.Client()434 response = await client.get('https://www.example.com/')435 print(response)Drop sync (#544)Tom Christiebefore adoption206c537
line 436why, one step back436Improve backend docs, particularly wrt. autodetection (#595)Tom Christiebefore adoptionf8794cb
lines 437–453why, one step back437asyncio.run(main())438```439440### [Trio](https://github.com/python-trio/trio)441442Trio is [an alternative async library](https://trio.readthedocs.io/en/stable/),443designed around the [the principles of structured concurrency](https://en.wikipedia.org/wiki/Structured_concurrency).444445```python446import httpx447import trio448449async def main():450 client = httpx.Client()451 response = await client.get('https://www.example.com/')452 print(response)453Drop sync (#544)Tom Christiebefore adoption206c537
lines 454–457why, one step back454trio.run(main)455```456457!!! importantImprove backend docs, particularly wrt. autodetection (#595)Tom Christiebefore adoptionf8794cb
line 458why, one step back458 The `trio` package must be installed to use the Trio backend.