advanced.md
This file
Path
docs/advanced.md
480 lines · 15.6 KiB · Markdown
Last changed by
Update advanced.mdTom Christiebefore adoptionddc4885
History of this fileTake it
The walk stopped after 200 commits: lines older than 9536b76 point at it.
Fail unit tests without 100% coverage (#206)Can Sarıgölbefore adoption9536b76
lines 1–8why, one step 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:15Fail unit tests without 100% coverage (#206)Can Sarıgölbefore adoption9536b76
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```pythonFail unit tests without 100% coverage (#206)Can Sarıgölbefore adoption9536b76
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...Fail unit tests without 100% coverage (#206)Can Sarıgölbefore adoption9536b76
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).74Fail unit tests without 100% coverage (#206)Can Sarıgölbefore adoption9536b76
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.Fail unit tests without 100% coverage (#206)Can Sarıgölbefore adoption9536b76
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.Fail unit tests without 100% coverage (#206)Can Sarıgölbefore adoption9536b76
line 84why, one step back84Drop sync (#544)Tom Christiebefore adoption206c537
line 85why, one step back85Let's take this Starlette application as an example:Fail unit tests without 100% coverage (#206)Can Sarıgölbefore adoption9536b76
lines 86–87why, one step back8687```pythonDrop sync (#544)Tom Christiebefore adoption206c537
lines 88–90why, one step back88from starlette.applications import Starlette89from starlette.responses import HTMLResponse90from starlette.routing import RouteFail unit tests without 100% coverage (#206)Can Sarıgölbefore adoption9536b76
lines 91–92why, one step back9192Drop sync (#544)Tom Christiebefore adoption206c537
lines 93–94why, one step back93async def hello():94 return HTMLResponse("Hello World!")Fail unit tests without 100% coverage (#206)Can Sarıgölbefore adoption9536b76
lines 95–96why, one step back9596Drop sync (#544)Tom Christiebefore adoption206c537
line 97why, one step back97app = Starlette(routes=[Route("/", hello)])Fail unit tests without 100% coverage (#206)Can Sarıgölbefore adoption9536b76
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 back176Warn if cert / verify / trust_env are passed to client.request() (#597)Tom Christiebefore adoption1e23855
lines 177–182why, one step back177When using `Client` instances, `trust_env` should be set on the client itself, rather that on the request methods:178179```python180client = httpx.Client(trust_env=False)181```182Add Unix Domain Sockets section to advanced docs page (#542)Jonas Lundbergbefore adoptionf0e6acb
lines 183–192why, one step back183## Unix Domain Sockets184185You can configure an `httpx` client to connect through a unix domain socket via the `uds` parameter. This is useful when making requests to a server that is bound to a socket file rather than an IP address.186187Here's an example requesting the Docker Engine API:188189```python190import httpx191192Drop sync (#544)Tom Christiebefore adoption206c537
line 193why, one step back193async with httpx.Client(uds="/var/run/docker.sock") as client:Add Unix Domain Sockets section to advanced docs page (#542)Jonas Lundbergbefore adoptionf0e6acb
line 194why, one step back194 # This request will connect through the socket file.Drop sync (#544)Tom Christiebefore adoption206c537
line 195why, one step back195 resp = await client.get("http://localhost/version")Add Unix Domain Sockets section to advanced docs page (#542)Jonas Lundbergbefore adoptionf0e6acb
lines 196–197why, one step back196```197Configure Proxy from Client (#353)Seth Michael Larsonbefore adoptionb0fa085
lines 198–204why, one step back198## HTTP Proxying199200HTTPX supports setting up proxies the same way that Requests does via the `proxies` parameter.201For example to forward all HTTP traffic to `http://127.0.0.1:3080` and all HTTPS traffic202to `http://127.0.0.1:3081` your `proxies` config would look like this:203204```pythonDocument client block-usage and close() (#487)Florimond Mancabefore adoption074cd25
lines 205–208why, one step back205>>> proxies = {206... "http": "http://127.0.0.1:3080",207... "https": "http://127.0.0.1:3081"208... }Drop sync (#544)Tom Christiebefore adoption206c537
line 209why, one step back209>>> async with httpx.Client(proxies=proxies) as client:Document client block-usage and close() (#487)Florimond Mancabefore adoption074cd25
line 210why, one step back210... ...Configure Proxy from Client (#353)Seth Michael Larsonbefore adoptionb0fa085
lines 211–217why, one step back211```212213Proxies can be configured for a specific scheme and host, all schemes of a host,214all hosts for a scheme, or for all requests. When determining which proxy configuration215to use for a given request this same order is used.216217```pythonDocument client block-usage and close() (#487)Florimond Mancabefore adoption074cd25
lines 218–223why, one step back218>>> proxies = {219... "http://example.com": "...", # Host+Scheme220... "all://example.com": "...", # Host221... "http": "...", # Scheme222... "all": "...", # All223... }Drop sync (#544)Tom Christiebefore adoption206c537
line 224why, one step back224>>> async with httpx.Client(proxies=proxies) as client:Document client block-usage and close() (#487)Florimond Mancabefore adoption074cd25
line 225why, one step back225... ...Drop sync (#544)Tom Christiebefore adoption206c537
line 226why, one step back226...Document client block-usage and close() (#487)Florimond Mancabefore adoption074cd25
line 227why, one step back227>>> proxy = "..." # Shortcut for {'all': '...'}Drop sync (#544)Tom Christiebefore adoption206c537
line 228why, one step back228>>> async with httpx.Client(proxies=proxy) as client:Document client block-usage and close() (#487)Florimond Mancabefore adoption074cd25
line 229why, one step back229... ...Configure Proxy from Client (#353)Seth Michael Larsonbefore adoptionb0fa085
lines 230–247why, one step back230```231232!!! warning233 To make sure that proxies cannot read your traffic,234 and even if the proxy_url uses HTTPS, it is recommended to235 use HTTPS and tunnel requests if possible.236237By default `HTTPProxy` will operate as a forwarding proxy for `http://...` requests238and will establish a `CONNECT` TCP tunnel for `https://` requests. This doesn't change239regardless of the `proxy_url` being `http` or `https`.240241Proxies can be configured to have different behavior such as forwarding or tunneling all requests:242243```python244proxy = httpx.HTTPProxy(245 proxy_url="https://127.0.0.1",246 proxy_mode=httpx.HTTPProxyMode.TUNNEL_ONLY247)Drop sync (#544)Tom Christiebefore adoption206c537
line 248why, one step back248async with httpx.Client(proxies=proxy) as client:Document client block-usage and close() (#487)Florimond Mancabefore adoption074cd25
line 249why, one step back249 # This request will be tunneled instead of forwarded.Drop sync (#544)Tom Christiebefore adoption206c537
line 250why, one step back250 r = await client.get("http://example.com")Configure Proxy from Client (#353)Seth Michael Larsonbefore adoptionb0fa085
line 251why, one step back251```Add timeout fine-tunning advanced docs section (#476)Mateusz Wośbefore adoption2984499
line 252why, one step back252Drop proxies parameter from the high level API (#485)Yeray Diaz Diazbefore adoption09db6ec
lines 253–257why, one step back253!!! note254255 Per request proxy configuration, i.e. `client.get(url, proxies=...)`,256 has not been implemented yet. To use proxies you must pass the proxy257 information at `Client` initialization.Add timeout fine-tunning advanced docs section (#476)Mateusz Wośbefore adoption2984499
line 258why, one step back258Allow default+override timeout style (#593)Tom Christiebefore adoption2f54b20
line 259why, one step back259## Timeout ConfigurationAdd timeout fine-tunning advanced docs section (#476)Mateusz Wośbefore adoption2984499
line 260why, one step back260Allow default+override timeout style (#593)Tom Christiebefore adoption2f54b20
line 261why, one step back261HTTPX is careful to enforce timeouts everywhere by default.Add timeout fine-tunning advanced docs section (#476)Mateusz Wośbefore adoption2984499
line 262why, one step back262Allow default+override timeout style (#593)Tom Christiebefore adoption2f54b20
lines 263–264why, one step back263The default behavior is to raise a `TimeoutException` after 5 seconds of264network inactivity.Multipart files tweaks (#482)Yeray Diaz Diazbefore adoption9ec2cfc
line 265why, one step back265Allow default+override timeout style (#593)Tom Christiebefore adoption2f54b20
line 266why, one step back266### Setting and disabling timeoutsMultipart files tweaks (#482)Yeray Diaz Diazbefore adoption9ec2cfc
line 267why, one step back267Allow default+override timeout style (#593)Tom Christiebefore adoption2f54b20
line 268why, one step back268You can set timeouts for an individual request:Add timeout fine-tunning advanced docs section (#476)Mateusz Wośbefore adoption2984499
lines 269–270why, one step back269270```pythonAllow default+override timeout style (#593)Tom Christiebefore adoption2f54b20
lines 271–272why, one step back271# Using the top-level API:272await httpx.get('http://example.com/api/v1/example', timeout=10.0)Add timeout fine-tunning advanced docs section (#476)Mateusz Wośbefore adoption2984499
line 273why, one step back273Allow default+override timeout style (#593)Tom Christiebefore adoption2f54b20
line 274why, one step back274# Using a client instance:Drop sync (#544)Tom Christiebefore adoption206c537
line 275why, one step back275async with httpx.Client() as client:Allow default+override timeout style (#593)Tom Christiebefore adoption2f54b20
line 276why, one step back276 await client.get("http://example.com/api/v1/example", timeout=10.0)Add timeout fine-tunning advanced docs section (#476)Mateusz Wośbefore adoption2984499
lines 277–278why, one step back277```278Allow default+override timeout style (#593)Tom Christiebefore adoption2f54b20
line 279why, one step back279Or disable timeouts for an individual request:Add timeout fine-tunning advanced docs section (#476)Mateusz Wośbefore adoption2984499
lines 280–281why, one step back280281```pythonAllow default+override timeout style (#593)Tom Christiebefore adoption2f54b20
lines 282–287why, one step back282# Using the top-level API:283await httpx.get('http://example.com/api/v1/example', timeout=None)284285# Using a client instance:286async with httpx.Client() as client:287 await client.get("http://example.com/api/v1/example", timeout=None)Add timeout fine-tunning advanced docs section (#476)Mateusz Wośbefore adoption2984499
lines 288–289why, one step back288```289Allow default+override timeout style (#593)Tom Christiebefore adoption2f54b20
line 290why, one step back290### Setting a default timeout on a clientAdd timeout fine-tunning advanced docs section (#476)Mateusz Wośbefore adoption2984499
line 291why, one step back291Allow default+override timeout style (#593)Tom Christiebefore adoption2f54b20
lines 292–293why, one step back292You can set a timeout on a client instance, which results in the given293`timeout` being used as the default for requests made with this client:Add timeout fine-tunning advanced docs section (#476)Mateusz Wośbefore adoption2984499
lines 294–295why, one step back294295```pythonAllow default+override timeout style (#593)Tom Christiebefore adoption2f54b20
lines 296–298why, one step back296client = httpx.Client() # Use a default 5s timeout everywhere.297client = httpx.Client(timeout=10.0) # Use a default 10s timeout everywhere.298client = httpx.Client(timeout=None) # Disable all timeouts by default.Add timeout fine-tunning advanced docs section (#476)Mateusz Wośbefore adoption2984499
lines 299–300why, one step back299```300Allow default+override timeout style (#593)Tom Christiebefore adoption2f54b20
line 301why, one step back301### Fine tuning the configurationMultipart files tweaks (#482)Yeray Diaz Diazbefore adoption9ec2cfc
line 302why, one step back302Allow default+override timeout style (#593)Tom Christiebefore adoption2f54b20
line 303why, one step back303HTTPX also allows you to specify the timeout behavior in more fine grained detail.Multipart files tweaks (#482)Yeray Diaz Diazbefore adoption9ec2cfc
line 304why, one step back304Allow default+override timeout style (#593)Tom Christiebefore adoption2f54b20
lines 305–306why, one step back305There are four different types of timeouts that may occur. These are **connect**,306**read**, **write**, and **pool** timeouts.Multipart files tweaks (#482)Yeray Diaz Diazbefore adoption9ec2cfc
line 307why, one step back307Allow default+override timeout style (#593)Tom Christiebefore adoption2f54b20
lines 308–321why, one step back308* The **connect timeout** specifies the maximum amount of time to wait until309a connection to the requested host is established. If HTTPX is unable to connect310within this time frame, a `ConnectTimeout` exception is raised.311* The **read timeout** specifies the maximum duration to wait for a chunk of312data to be received (for example, a chunk of the response body). If HTTPX is313unable to receive data within this time frame, a `ReadTimeout` exception is raised.314* The **write timeout** specifies the maximum duration to wait for a chunk of315data to be sent (for example, a chunk of the request body). If HTTPX is unable316to send data within this time frame, a `WriteTimeout` exception is raised.317* The **pool timeout** specifies the maximum duration to wait for acquiring318a connection from the connection pool. If HTTPX is unable to acquire a connection319within this time frame, a `PoolTimeout` exception is raised. A related320configuration here is the maximum number of allowable connections in the321connection pool, which is configured by the `pool_limits`.Add timeout fine-tunning advanced docs section (#476)Mateusz Wośbefore adoption2984499
line 322why, one step back322Allow default+override timeout style (#593)Tom Christiebefore adoption2f54b20
lines 323–324why, one step back323You can configure the timeout behavior for any of these values...324Add timeout fine-tunning advanced docs section (#476)Mateusz Wośbefore adoption2984499
line 325why, one step back325```pythonAllow default+override timeout style (#593)Tom Christiebefore adoption2f54b20
lines 326–328why, one step back326# A client with a 60s timeout for connecting, and a 10s timeout elsewhere.327timeout = httpx.Timeout(10.0, connect_timeout=60.0)328client = httpx.Client(timeout=timeout)Add timeout fine-tunning advanced docs section (#476)Mateusz Wośbefore adoption2984499
line 329why, one step back329Allow default+override timeout style (#593)Tom Christiebefore adoption2f54b20
line 330why, one step back330response = await client.get('http://example.com/')Multipart files tweaks (#482)Yeray Diaz Diazbefore adoption9ec2cfc
lines 331–336why, one step back331```332333## Multipart file encoding334335As mentioned in the [quickstart](/quickstart#sending-multipart-file-uploads)336multipart file encoding is available by passing a dictionary with theClarify multipart documentation (#580)Mattwmaster58before adoption33cb397
line 337why, one step back337name of the payloads as keys and either tuple of elements or a file-like object or a string as values.Multipart files tweaks (#482)Yeray Diaz Diazbefore adoption9ec2cfc
lines 338–340why, one step back338339```python340>>> files = {'upload-file': ('report.xls', open('report.xls', 'rb'), 'application/vnd.ms-excel')}Drop sync (#544)Tom Christiebefore adoption206c537
line 341why, one step back341>>> r = await httpx.post("https://httpbin.org/post", files=files)Multipart files tweaks (#482)Yeray Diaz Diazbefore adoption9ec2cfc
lines 342–351why, one step back342>>> print(r.text)343{344 ...345 "files": {346 "upload-file": "<... binary content ...>"347 },348 ...349}350```351Clarify multipart documentation (#580)Mattwmaster58before adoption33cb397
line 352why, one step back352More specifically, if a tuple is used as a value, it must have between 2 and 3 elements:Update advanced.mdTom Christiebefore adoption871b0b5
line 353why, one step back353Clarify multipart documentation (#580)Mattwmaster58before adoption33cb397
lines 354–355why, one step back354- The first element is an optional file name which can be set to `None`.355- The second element may be a file-like object or a string which will be automaticallyMultipart files tweaks (#482)Yeray Diaz Diazbefore adoption9ec2cfc
line 356why, one step back356encoded in UTF-8.Better HTTP/2 defaults. (#584)Tom Christiebefore adoption30229f1
line 357why, one step back357- An optional third element can be used to specify theMultipart files tweaks (#482)Yeray Diaz Diazbefore adoption9ec2cfc
line 358why, one step back358[MIME type](https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/MIME_Types)Clarify multipart documentation (#580)Mattwmaster58before adoption33cb397
lines 359–362why, one step back359of the file being uploaded. If not specified HTTPX will attempt to guess the MIME type based360on the file name, with unknown file extensions defaulting to "application/octet-stream".361If the file name is explicitly set to `None` then HTTPX will not include a content-type362MIME header field.Multipart files tweaks (#482)Yeray Diaz Diazbefore adoption9ec2cfc
lines 363–365why, one step back363364```python365>>> files = {'upload-file': (None, 'text content', 'text/plain')}Drop sync (#544)Tom Christiebefore adoption206c537
line 366why, one step back366>>> r = await httpx.post("https://httpbin.org/post", files=files)Multipart files tweaks (#482)Yeray Diaz Diazbefore adoption9ec2cfc
lines 367–375why, one step back367>>> print(r.text)368{369 ...370 "files": {},371 "form": {372 "upload-file": "text-content"373 },374 ...375}Add timeout fine-tunning advanced docs section (#476)Mateusz Wośbefore adoption2984499
line 376why, one step back376```Add docs on SSL certificates (#510)Florimond Mancabefore adoption95b2b24
lines 377–381why, one step back377378## SSL certificates379380When making a request over HTTPS, HTTPX needs to verify the identity of the requested host. To do this, it uses a bundle of SSL certificates (a.k.a. CA bundle) delivered by a trusted certificate authority (CA).381Warn if cert / verify / trust_env are passed to client.request() (#597)Tom Christiebefore adoption1e23855
line 382why, one step back382### Changing the verification defaultsAdd docs on SSL certificates (#510)Florimond Mancabefore adoption95b2b24
lines 383–385why, one step back383384By 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.385Warn if cert / verify / trust_env are passed to client.request() (#597)Tom Christiebefore adoption1e23855
line 386why, one step back386If you'd like to use a custom CA bundle, you can use the `verify` parameter.Add docs on SSL certificates (#510)Florimond Mancabefore adoption95b2b24
lines 387–390why, one step back387388```python389import httpx390Drop sync (#544)Tom Christiebefore adoption206c537
line 391why, one step back391r = await httpx.get("https://example.org", verify="path/to/client.pem")Add docs on SSL certificates (#510)Florimond Mancabefore adoption95b2b24
line 392why, one step back392```Warn if cert / verify / trust_env are passed to client.request() (#597)Tom Christiebefore adoption1e23855
lines 393–409why, one step back393394You can also disable the SSL verification...395396```python397import httpx398399r = await httpx.get("https://example.org", verify=False)400```401402### SSL configuration on client instances403404If you're using a `Client()` instance, then you should pass any SSL settings when instantiating the client.405406```python407client = httpx.Client(verify=False)408```409Update advanced.mdTom Christiebefore adoptionddc4885
line 410why, one step back410The `client.get(...)` method and other request methods *do not* support changing the SSL settings on a per-request basis. If you need different SSL settings in different cases you should use more that one client instance, with different settings on each. Each client will then be using an isolated connection pool with a specific fixed SSL configuration on all connections within that pool.Add docs on SSL certificates (#510)Florimond Mancabefore adoption95b2b24
lines 411–423why, one step back411412### Making HTTPS requests to a local server413414When making requests to local servers, such as a development server running on `localhost`, you will typically be using unencrypted HTTP connections.415416If you do need to make HTTPS connections to a local server, for example to test an HTTPS-only service, you will need to create and use your own certificates. Here's one way to do it:4174181. Use [trustme-cli](https://github.com/sethmlarson/trustme-cli/) to generate a pair of server key/cert files, and a client cert file.4191. Pass the server key/cert files when starting your local server. (This depends on the particular web server you're using. For example, [Uvicorn](https://www.uvicorn.org) provides the `--ssl-keyfile` and `--ssl-certfile` options.)4201. Tell HTTPX to use the certificates stored in `client.pem`:421422```python423>>> import httpxDrop sync (#544)Tom Christiebefore adoption206c537
line 424why, one step back424>>> r = await httpx.get("https://localhost:8000", verify="/tmp/client.pem")Add docs on SSL certificates (#510)Florimond Mancabefore adoption95b2b24
lines 425–427why, one step back425>>> r426Response <200 OK>427```Drop sync (#544)Tom Christiebefore adoption206c537
line 428why, one step back428Improve backend docs, particularly wrt. autodetection (#595)Tom Christiebefore adoptionf8794cb
line 429why, one step back429## Supported async environmentsDrop sync (#544)Tom Christiebefore adoption206c537
line 430why, one step back430Improve backend docs, particularly wrt. autodetection (#595)Tom Christiebefore adoptionf8794cb
line 431why, one step back431HTTPX supports either `asyncio` or `trio` as an async environment.Drop sync (#544)Tom Christiebefore adoption206c537
line 432why, one step back432Improve backend docs, particularly wrt. autodetection (#595)Tom Christiebefore adoptionf8794cb
lines 433–434why, one step back433By default it will auto-detect which of those two to use as the backend434for socket operations and concurrency primitives.Drop sync (#544)Tom Christiebefore adoption206c537
line 435why, one step back435Improve backend docs, particularly wrt. autodetection (#595)Tom Christiebefore adoptionf8794cb
lines 436–437why, one step back436You can also explicitly select a backend by instantiating a client with the437`backend` argument...Drop sync (#544)Tom Christiebefore adoption206c537
line 438why, one step back438Improve backend docs, particularly wrt. autodetection (#595)Tom Christiebefore adoptionf8794cb
lines 439–443why, one step back439```python440client = httpx.Client(backend='auto') # Autodetection. The default case.441client = httpx.Client(backend='asyncio') # Use asyncio as the backend.442client = httpx.Client(backend='trio') # Use trio as the backend.443```Drop sync (#544)Tom Christiebefore adoption206c537
line 444why, one step back444Improve backend docs, particularly wrt. autodetection (#595)Tom Christiebefore adoptionf8794cb
lines 445–449why, one step back445### [AsyncIO](https://docs.python.org/3/library/asyncio.html)446447AsyncIO is Python's [built-in library](https://docs.python.org/3/library/asyncio.html)448for writing concurrent code with the async/await syntax.449Drop sync (#544)Tom Christiebefore adoption206c537
line 450why, one step back450```pythonImprove backend docs, particularly wrt. autodetection (#595)Tom Christiebefore adoptionf8794cb
line 451why, one step back451import asyncioDrop sync (#544)Tom Christiebefore adoption206c537
lines 452–454why, one step back452import httpx453454async def main():Improve backend docs, particularly wrt. autodetection (#595)Tom Christiebefore adoptionf8794cb
lines 455–457why, one step back455 client = httpx.Client()456 response = await client.get('https://www.example.com/')457 print(response)Drop sync (#544)Tom Christiebefore adoption206c537
line 458why, one step back458Improve backend docs, particularly wrt. autodetection (#595)Tom Christiebefore adoptionf8794cb
lines 459–475why, one step back459asyncio.run(main())460```461462### [Trio](https://github.com/python-trio/trio)463464Trio is [an alternative async library](https://trio.readthedocs.io/en/stable/),465designed around the [the principles of structured concurrency](https://en.wikipedia.org/wiki/Structured_concurrency).466467```python468import httpx469import trio470471async def main():472 client = httpx.Client()473 response = await client.get('https://www.example.com/')474 print(response)475Drop sync (#544)Tom Christiebefore adoption206c537
lines 476–479why, one step back476trio.run(main)477```478479!!! importantImprove backend docs, particularly wrt. autodetection (#595)Tom Christiebefore adoptionf8794cb
line 480why, one step back480 The `trio` package must be installed to use the Trio backend.