Skip to content
Myr

advanced.md

This file

Path

docs/advanced.md

473 lines · 16.1 KiB · Markdown

Last changed by

Detect credentials in proxy URLs and create Proxy-authorization header (#780)Yeray Diaz Diazbefore adoptionb234203

History of this file

Take it

The walk stopped after 200 commits: lines older than 31730e7 point at it.

Add documentation for requests.Session compatibility (#449)Jt Miclatbefore adoption31730e7

lines 1–8why, one step back
1# Advanced Usage
23## Client Instances45Using a Client instance to make requests will give you HTTP connection pooling,6will provide cookie persistence, and allows you to apply configuration across7all outgoing requests.8

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

lines 9–10why, one step back
9!!! hint
10 A Client instance is equivalent to a Session instance in `requests`.

Add documentation for requests.Session compatibility (#449)Jt Miclatbefore adoption31730e7

line 11why, one step back
11

Rename Client to AsyncClient (with compat synonym) (#680)Florimond Mancabefore adoptione284b84

lines 12–16why, one step back
12!!! note
13 Starting from `httpx==0.10.0`, the default and recommended Client class is `AsyncClient`. This should help prevent breaking changes once sync support is reintroduced.1415 A `Client` synonym remains for compatibility with `httpx==0.9.*` releases, but you are encouraged to migrate to `AsyncClient` as soon as possible.16

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

lines 17–20why, one step back
17### Usage
1819The 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:20

Add documentation for requests.Session compatibility (#449)Jt Miclatbefore adoption31730e7

line 21why, one step back
21```python

Proposed 0.11 docs (#727)Tom Christiebefore adoption2038919

lines 22–23why, one step back
22>>> with httpx.Client() as client:
23... r = client.get('https://example.com')

Drop sync (#544)Tom Christiebefore adoption206c537

line 24why, one step back
24...

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

lines 25–28why, one step back
25>>> r
26<Response [200 OK]>27```28

Proposed 0.11 docs (#727)Tom Christiebefore adoption2038919

line 29why, one step back
29Alternatively, you can explicitly close the connection pool without block-usage using `.close()`:

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

lines 30–31why, one step back
30
31```python

Proposed 0.11 docs (#727)Tom Christiebefore adoption2038919

line 32why, one step back
32>>> client = httpx.Client()

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

line 33why, one step back
33>>> try:

Proposed 0.11 docs (#727)Tom Christiebefore adoption2038919

line 34why, one step back
34... r = client.get('https://example.com')

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

line 35why, one step back
35... finally:

Proposed 0.11 docs (#727)Tom Christiebefore adoption2038919

line 36why, one step back
36... client.close()

Drop sync (#544)Tom Christiebefore adoption206c537

line 37why, one step back
37...

Add documentation for requests.Session compatibility (#449)Jt Miclatbefore adoption31730e7

lines 38–41why, one step back
38>>> r
39<Response [200 OK]>40```41

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

lines 42–43why, one step back
42Once you have a `Client`, you can use all the features documented in the [Quickstart](/quickstart) guide.
43

Document client-level configuration (#488)Florimond Mancabefore adoptionad38db8

lines 44–52why, one step back
44### Configuration
4546Clients allow you to apply configuration to all outgoing requests by passing parameters to the `Client` constructor.4748For example, to apply a set of custom headers on every request:4950```python51>>> url = 'http://httpbin.org/headers'52>>> headers = {'user-agent': 'my-app/0.0.1'}

Proposed 0.11 docs (#727)Tom Christiebefore adoption2038919

lines 53–54why, one step back
53>>> with httpx.Client(headers=headers) as client:
54... r = client.get(url)

Document client-level configuration (#488)Florimond Mancabefore adoptionad38db8

lines 55–61why, one step back
55...
56>>> r.json()['headers']['User-Agent']57'my-app/0.0.1'58```5960!!! note61 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 62why, one step back
62

Document client-level configuration (#488)Florimond Mancabefore adoptionad38db8

lines 63–65why, one step back
63 - For headers, query parameters and cookies, the values are merged into one.
64 - For all other parameters, the request-level value is used.65

Rename Client to AsyncClient (with compat synonym) (#680)Florimond Mancabefore adoptione284b84

line 66why, one step back
66Additionally, `Client` accepts some parameters that aren't available at the request level.

Document client-level configuration (#488)Florimond Mancabefore adoptionad38db8

lines 67–70why, one step back
67
68One particularly useful parameter is `base_url`, which allows you to define a base URL to prepend to all outgoing requests:6970```python

Proposed 0.11 docs (#727)Tom Christiebefore adoption2038919

lines 71–72why, one step back
71>>> with httpx.Client(base_url='http://httpbin.org') as client:
72... r = client.get('/headers')

Drop sync (#544)Tom Christiebefore adoption206c537

line 73why, one step back
73...

Document client-level configuration (#488)Florimond Mancabefore adoptionad38db8

lines 74–77why, one step back
74>>> r.request.url
75URL('http://httpbin.org/headers')76```77

Proposed 0.11 docs (#727)Tom Christiebefore adoption2038919

line 78why, one step back
78For a list of all available client-level parameters, see the [`Client` API reference](/api/#client).

Document client-level configuration (#488)Florimond Mancabefore adoptionad38db8

line 79why, one step back
79

Add documentation for requests.Session compatibility (#449)Jt Miclatbefore adoption31730e7

lines 80–81why, one step back
80## Calling into Python Web Apps
81

Proposed 0.11 docs (#727)Tom Christiebefore adoption2038919

line 82why, one step back
82You can configure an `httpx` client to call directly into a Python web application using the WSGI protocol.

Add documentation for requests.Session compatibility (#449)Jt Miclatbefore adoption31730e7

lines 83–85why, one step back
83
84This is particularly useful for two main use-cases:85

Fix some grammar in Advanced docs (#461)thebigmunchbefore adoption3917866

lines 86–87why, one step back
86* Using `httpx` as a client inside test cases.
87* Mocking out external services during tests or in dev/staging environments.

Add documentation for requests.Session compatibility (#449)Jt Miclatbefore adoption31730e7

line 88why, one step back
88

Proposed 0.11 docs (#727)Tom Christiebefore adoption2038919

line 89why, one step back
89Here's an example of integrating against a Flask application:

Add documentation for requests.Session compatibility (#449)Jt Miclatbefore adoption31730e7

lines 90–91why, one step back
90
91```python

Proposed 0.11 docs (#727)Tom Christiebefore adoption2038919

lines 92–93why, one step back
92from flask import Flask
93import httpx

Add documentation for requests.Session compatibility (#449)Jt Miclatbefore adoption31730e7

lines 94–95why, one step back
94
95

Proposed 0.11 docs (#727)Tom Christiebefore adoption2038919

line 96why, one step back
96app = Flask(__name__)

Add documentation for requests.Session compatibility (#449)Jt Miclatbefore adoption31730e7

line 97why, one step back
97

Proposed 0.11 docs (#727)Tom Christiebefore adoption2038919

lines 98–100why, one step back
98@app.route("/")
99def hello():100 return "Hello World!"

Add documentation for requests.Session compatibility (#449)Jt Miclatbefore adoption31730e7

line 101why, one step back
101

Proposed 0.11 docs (#727)Tom Christiebefore adoption2038919

lines 102–105why, one step back
102with httpx.Client(app=app) as client:
103 r = client.get('http://example/')104 assert r.status_code == 200105 assert r.text == "Hello World!"

Add documentation for requests.Session compatibility (#449)Jt Miclatbefore adoption31730e7

lines 106–107why, one step back
106```
107

Proposed 0.11 docs (#727)Tom Christiebefore adoption2038919

line 108why, one step back
108For some more complex cases you might need to customize the WSGI dispatch. This allows you to:

Drop sync (#544)Tom Christiebefore adoption206c537

line 109why, one step back
109

Fix some grammar in Advanced docs (#461)thebigmunchbefore adoption3917866

line 110why, one step back
110* Inspect 500 error responses rather than raise exceptions by setting `raise_app_exceptions=False`.

Proposed 0.11 docs (#727)Tom Christiebefore adoption2038919

lines 111–112why, one step back
111* Mount the WSGI application at a subpath by setting `script_name` (WSGI).
112* Use a given client address for requests by setting `remote_addr` (WSGI).

Add documentation for requests.Session compatibility (#449)Jt Miclatbefore adoption31730e7

lines 113–116why, one step back
113
114For example:115116```python

Proposed 0.11 docs (#727)Tom Christiebefore adoption2038919

lines 117–119why, one step back
117# Instantiate a client that makes WSGI requests with a client IP of "1.2.3.4".
118dispatch = httpx.WSGIDispatch(app=app, remote_addr="1.2.3.4")119with httpx.Client(dispatch=dispatch) as client:

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

line 120why, one step back
120 ...

Add documentation for requests.Session compatibility (#449)Jt Miclatbefore adoption31730e7

line 121why, one step back
121```

Link to ASGI docs (#577)Tom Christiebefore adoption7d45db0

line 122why, one step back
122

Add documentation for requests.Session compatibility (#449)Jt Miclatbefore adoption31730e7

lines 123–128why, one step back
123## Build Request
124125You can use `Client.build_request()` to build a request and126make modifications before sending the request.127128```python

Proposed 0.11 docs (#727)Tom Christiebefore adoption2038919

line 129why, one step back
129>>> with httpx.Client() as client:

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

lines 130–131why, one step back
130... req = client.build_request("OPTIONS", "https://example.com")
131... req.url.full_path = "*" # Build an 'OPTIONS *' request for CORS

Proposed 0.11 docs (#727)Tom Christiebefore adoption2038919

line 132why, one step back
132... r = client.send(req)

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

lines 133–134why, one step back
133...
134>>> r

Add documentation for requests.Session compatibility (#449)Jt Miclatbefore adoption31730e7

lines 135–142why, one step back
135<Response [200 OK]>
136```137138## .netrc Support139140HTTPX supports .netrc file. In `trust_env=True` cases, if auth parameter is141not defined, HTTPX tries to add auth into request's header from .netrc file.142

Cache netrc authentication per-client (#400)Can Sarıgölbefore adoption84731c8

lines 143–147why, one step back
143!!! note
144 The NETRC file is cached across requests made by a client.145 If you need to refresh the cache (e.g. because the NETRC file has changed),146 you should create a new client or restart the interpreter.147

Add documentation for requests.Session compatibility (#449)Jt Miclatbefore adoption31730e7

lines 148–149why, one step back
148As default `trust_env` is true. To set false:
149```python

Proposed 0.11 docs (#727)Tom Christiebefore adoption2038919

line 150why, one step back
150>>> httpx.get('https://example.org/', trust_env=False)

Add documentation for requests.Session compatibility (#449)Jt Miclatbefore adoption31730e7

lines 151–170why, one step back
151```
152153If `NETRC` environment is empty, HTTPX tries to use default files.154(`~/.netrc`, `~/_netrc`)155156To change `NETRC` environment:157```python158>>> import os159>>> os.environ["NETRC"] = "my_default_folder/.my_netrc"160```161162.netrc file content example:163```164machine netrcexample.org165login example-username166password example-password167168...169```170

Warn if cert / verify / trust_env are passed to client.request() (#597)Tom Christiebefore adoption1e23855

lines 171–173why, one step back
171When using `Client` instances, `trust_env` should be set on the client itself, rather that on the request methods:
172173```python

Proposed 0.11 docs (#727)Tom Christiebefore adoption2038919

line 174why, one step back
174client = httpx.Client(trust_env=False)

Warn if cert / verify / trust_env are passed to client.request() (#597)Tom Christiebefore adoption1e23855

lines 175–176why, one step back
175```
176

Add documentation for requests.Session compatibility (#449)Jt Miclatbefore adoption31730e7

lines 177–178why, one step back
177## HTTP Proxying
178

Detect credentials in proxy URLs and create Proxy-authorization header (#780)Yeray Diaz Diazbefore adoptionb234203

line 179why, one step back
179HTTPX supports setting up HTTP proxies the same way that Requests does via the `proxies` parameter.

Add documentation for requests.Session compatibility (#449)Jt Miclatbefore adoption31730e7

lines 180–183why, one step back
180For example to forward all HTTP traffic to `http://127.0.0.1:3080` and all HTTPS traffic
181to `http://127.0.0.1:3081` your `proxies` config would look like this:182183```python

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

lines 184–187why, one step back
184>>> proxies = {
185... "http": "http://127.0.0.1:3080",186... "https": "http://127.0.0.1:3081"187... }

Proposed 0.11 docs (#727)Tom Christiebefore adoption2038919

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

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

line 189why, one step back
189... ...

Add documentation for requests.Session compatibility (#449)Jt Miclatbefore adoption31730e7

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

Detect credentials in proxy URLs and create Proxy-authorization header (#780)Yeray Diaz Diazbefore adoptionb234203

lines 192–194why, one step back
192Credentials may be passed in as part of the URL in the standard way, i.e.
193`http://username:password@127.0.0.1:3080`.194

Add documentation for requests.Session compatibility (#449)Jt Miclatbefore adoption31730e7

lines 195–199why, one step back
195Proxies can be configured for a specific scheme and host, all schemes of a host,
196all hosts for a scheme, or for all requests. When determining which proxy configuration197to use for a given request this same order is used.198199```python

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

lines 200–205why, one step back
200>>> proxies = {
201... "http://example.com": "...", # Host+Scheme202... "all://example.com": "...", # Host203... "http": "...", # Scheme204... "all": "...", # All205... }

Proposed 0.11 docs (#727)Tom Christiebefore adoption2038919

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

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

line 207why, one step back
207... ...

Drop sync (#544)Tom Christiebefore adoption206c537

line 208why, one step back
208...

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

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

Proposed 0.11 docs (#727)Tom Christiebefore adoption2038919

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

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

line 211why, one step back
211... ...

Add documentation for requests.Session compatibility (#449)Jt Miclatbefore adoption31730e7

lines 212–218why, one step back
212```
213214!!! warning215 To make sure that proxies cannot read your traffic,216 and even if the proxy_url uses HTTPS, it is recommended to217 use HTTPS and tunnel requests if possible.218

Proposed 0.11 docs (#727)Tom Christiebefore adoption2038919

line 219why, one step back
219By default `httpx.Proxy` will operate as a forwarding proxy for `http://...` requests

Add documentation for requests.Session compatibility (#449)Jt Miclatbefore adoption31730e7

line 220why, one step back
220and will establish a `CONNECT` TCP tunnel for `https://` requests. This doesn't change

Proposed 0.11 docs (#727)Tom Christiebefore adoption2038919

line 221why, one step back
221regardless of the proxy `url` being `http` or `https`.

Add documentation for requests.Session compatibility (#449)Jt Miclatbefore adoption31730e7

lines 222–225why, one step back
222
223Proxies can be configured to have different behavior such as forwarding or tunneling all requests:224225```python

Proposed 0.11 docs (#727)Tom Christiebefore adoption2038919

lines 226–228why, one step back
226proxy = httpx.Proxy(
227 url="https://127.0.0.1",228 mode="TUNNEL_ONLY" # May be "TUNNEL_ONLY" or "FORWARD_ONLY". Defaults to "DEFAULT".

Add documentation for requests.Session compatibility (#449)Jt Miclatbefore adoption31730e7

line 229why, one step back
229)

Proposed 0.11 docs (#727)Tom Christiebefore adoption2038919

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

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

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

Proposed 0.11 docs (#727)Tom Christiebefore adoption2038919

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

Add documentation for requests.Session compatibility (#449)Jt Miclatbefore adoption31730e7

line 233why, one step back
233```

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

line 234why, one step back
234

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

lines 235–236why, one step back
235!!! note
236

Proposed 0.11 docs (#727)Tom Christiebefore adoption2038919

lines 237–238why, one step back
237 To use proxies you must pass the proxy information at `Client` initialization,
238 rather than on the `.get(...)` call or other request methods.

Detect credentials in proxy URLs and create Proxy-authorization header (#780)Yeray Diaz Diazbefore adoptionb234203

lines 239–240why, one step back
239
240SOCKS proxies are *not* supported yet.

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

line 241why, one step back
241

Allow default+override timeout style (#593)Tom Christiebefore adoption2f54b20

line 242why, one step back
242## Timeout Configuration

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

line 243why, one step back
243

Allow default+override timeout style (#593)Tom Christiebefore adoption2f54b20

line 244why, one step back
244HTTPX is careful to enforce timeouts everywhere by default.

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

line 245why, one step back
245

Allow default+override timeout style (#593)Tom Christiebefore adoption2f54b20

lines 246–247why, one step back
246The default behavior is to raise a `TimeoutException` after 5 seconds of
247network inactivity.

Multipart files tweaks (#482)Yeray Diaz Diazbefore adoption9ec2cfc

line 248why, one step back
248

Allow default+override timeout style (#593)Tom Christiebefore adoption2f54b20

line 249why, one step back
249### Setting and disabling timeouts

Multipart files tweaks (#482)Yeray Diaz Diazbefore adoption9ec2cfc

line 250why, one step back
250

Allow default+override timeout style (#593)Tom Christiebefore adoption2f54b20

line 251why, one step back
251You can set timeouts for an individual request:

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

lines 252–253why, one step back
252
253```python

Allow default+override timeout style (#593)Tom Christiebefore adoption2f54b20

line 254why, one step back
254# Using the top-level API:

Proposed 0.11 docs (#727)Tom Christiebefore adoption2038919

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

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

line 256why, one step back
256

Allow default+override timeout style (#593)Tom Christiebefore adoption2f54b20

line 257why, one step back
257# Using a client instance:

Proposed 0.11 docs (#727)Tom Christiebefore adoption2038919

lines 258–259why, one step back
258with httpx.Client() as client:
259 client.get("http://example.com/api/v1/example", timeout=10.0)

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

lines 260–261why, one step back
260```
261

Allow default+override timeout style (#593)Tom Christiebefore adoption2f54b20

line 262why, one step back
262Or disable timeouts for an individual request:

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

lines 263–264why, one step back
263
264```python

Allow default+override timeout style (#593)Tom Christiebefore adoption2f54b20

line 265why, one step back
265# Using the top-level API:

Proposed 0.11 docs (#727)Tom Christiebefore adoption2038919

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

Allow default+override timeout style (#593)Tom Christiebefore adoption2f54b20

lines 267–268why, one step back
267
268# Using a client instance:

Proposed 0.11 docs (#727)Tom Christiebefore adoption2038919

lines 269–270why, one step back
269with httpx.Client() as client:
270 client.get("http://example.com/api/v1/example", timeout=None)

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

lines 271–272why, one step back
271```
272

Allow default+override timeout style (#593)Tom Christiebefore adoption2f54b20

line 273why, one step back
273### Setting a default timeout on a client

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

line 274why, one step back
274

Allow default+override timeout style (#593)Tom Christiebefore adoption2f54b20

lines 275–276why, one step back
275You can set a timeout on a client instance, which results in the given
276`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 277–278why, one step back
277
278```python

Proposed 0.11 docs (#727)Tom Christiebefore adoption2038919

lines 279–281why, one step back
279client = httpx.Client() # Use a default 5s timeout everywhere.
280client = httpx.Client(timeout=10.0) # Use a default 10s timeout everywhere.281client = httpx.Client(timeout=None) # Disable all timeouts by default.

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

lines 282–283why, one step back
282```
283

Allow default+override timeout style (#593)Tom Christiebefore adoption2f54b20

line 284why, one step back
284### Fine tuning the configuration

Multipart files tweaks (#482)Yeray Diaz Diazbefore adoption9ec2cfc

line 285why, one step back
285

Allow default+override timeout style (#593)Tom Christiebefore adoption2f54b20

line 286why, one step back
286HTTPX also allows you to specify the timeout behavior in more fine grained detail.

Multipart files tweaks (#482)Yeray Diaz Diazbefore adoption9ec2cfc

line 287why, one step back
287

Allow default+override timeout style (#593)Tom Christiebefore adoption2f54b20

lines 288–289why, one step back
288There are four different types of timeouts that may occur. These are **connect**,
289**read**, **write**, and **pool** timeouts.

Multipart files tweaks (#482)Yeray Diaz Diazbefore adoption9ec2cfc

line 290why, one step back
290

Allow default+override timeout style (#593)Tom Christiebefore adoption2f54b20

lines 291–304why, one step back
291* The **connect timeout** specifies the maximum amount of time to wait until
292a connection to the requested host is established. If HTTPX is unable to connect293within this time frame, a `ConnectTimeout` exception is raised.294* The **read timeout** specifies the maximum duration to wait for a chunk of295data to be received (for example, a chunk of the response body). If HTTPX is296unable to receive data within this time frame, a `ReadTimeout` exception is raised.297* The **write timeout** specifies the maximum duration to wait for a chunk of298data to be sent (for example, a chunk of the request body). If HTTPX is unable299to send data within this time frame, a `WriteTimeout` exception is raised.300* The **pool timeout** specifies the maximum duration to wait for acquiring301a connection from the connection pool. If HTTPX is unable to acquire a connection302within this time frame, a `PoolTimeout` exception is raised. A related303configuration here is the maximum number of allowable connections in the304connection pool, which is configured by the `pool_limits`.

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

line 305why, one step back
305

Allow default+override timeout style (#593)Tom Christiebefore adoption2f54b20

lines 306–307why, one step back
306You can configure the timeout behavior for any of these values...
307

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

line 308why, one step back
308```python

Allow default+override timeout style (#593)Tom Christiebefore adoption2f54b20

lines 309–310why, one step back
309# A client with a 60s timeout for connecting, and a 10s timeout elsewhere.
310timeout = httpx.Timeout(10.0, connect_timeout=60.0)

Proposed 0.11 docs (#727)Tom Christiebefore adoption2038919

line 311why, one step back
311client = httpx.Client(timeout=timeout)

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

line 312why, one step back
312

Proposed 0.11 docs (#727)Tom Christiebefore adoption2038919

line 313why, one step back
313response = client.get('http://example.com/')

Multipart files tweaks (#482)Yeray Diaz Diazbefore adoption9ec2cfc

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

Clarify multipart documentation (#580)Mattwmaster58before adoption33cb397

line 320why, one step back
320name 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 321–323why, one step back
321
322```python323>>> files = {'upload-file': ('report.xls', open('report.xls', 'rb'), 'application/vnd.ms-excel')}

Proposed 0.11 docs (#727)Tom Christiebefore adoption2038919

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

Multipart files tweaks (#482)Yeray Diaz Diazbefore adoption9ec2cfc

lines 325–334why, one step back
325>>> print(r.text)
326{327 ...328 "files": {329 "upload-file": "<... binary content ...>"330 },331 ...332}333```334

Clarify multipart documentation (#580)Mattwmaster58before adoption33cb397

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

Update advanced.mdTom Christiebefore adoption871b0b5

line 336why, one step back
336

Clarify multipart documentation (#580)Mattwmaster58before adoption33cb397

lines 337–338why, one step back
337- The first element is an optional file name which can be set to `None`.
338- The second element may be a file-like object or a string which will be automatically

Multipart files tweaks (#482)Yeray Diaz Diazbefore adoption9ec2cfc

line 339why, one step back
339encoded in UTF-8.

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

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

Multipart files tweaks (#482)Yeray Diaz Diazbefore adoption9ec2cfc

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

Clarify multipart documentation (#580)Mattwmaster58before adoption33cb397

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

Multipart files tweaks (#482)Yeray Diaz Diazbefore adoption9ec2cfc

lines 346–348why, one step back
346
347```python348>>> files = {'upload-file': (None, 'text content', 'text/plain')}

Proposed 0.11 docs (#727)Tom Christiebefore adoption2038919

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

Multipart files tweaks (#482)Yeray Diaz Diazbefore adoption9ec2cfc

lines 350–358why, one step back
350>>> print(r.text)
351{352 ...353 "files": {},354 "form": {355 "upload-file": "text-content"356 },357 ...358}

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

line 359why, one step back
359```

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

line 360why, one step back
360

Public Auth API (#732)Tom Christiebefore adoption12dd157

lines 361–423why, one step back
361## Customizing authentication
362363When issuing requests or instantiating a client, the `auth` argument can be used to pass an authentication scheme to use. The `auth` argument may be one of the following...364365* A two-tuple of `username`/`password`, to be used with basic authentication.366* An instance of `httpx.BasicAuth()` or `httpx.DigestAuth()`.367* A callable, accepting a request and returning an authenticated request instance.368* A subclass of `httpx.Auth`.369370The most involved of these is the last, which allows you to create authentication flows involving one or more requests. A subclass of `httpx.Auth` should implement `def auth_flow(request)`, and yield any requests that need to be made...371372```python373class MyCustomAuth(httpx.Auth):374 def __init__(self, token):375 self.token = token376377 def auth_flow(self, request):378 # Send the request, with a custom `X-Authentication` header.379 request.headers['X-Authentication'] = self.token380 yield request381```382383If the auth flow requires more that one request, you can issue multiple yields, and obtain the response in each case...384385```python386class MyCustomAuth(httpx.Auth):387 def __init__(self, token):388 self.token = token389390 def auth_flow(self, request):391 response = yield request392 if response.status_code == 401:393 # If the server issues a 401 response then resend the request,394 # with a custom `X-Authentication` header.395 request.headers['X-Authentication'] = self.token396 yield request397```398399Custom authentication classes are designed to not perform any I/O, so that they may be used with both sync and async client instances. If you are implementing an authentication scheme that requires the request body, then you need to indicate this on the class using a `requires_request_body` property.400401You will then be able to access `request.content` inside the `.auth_flow()` method.402403```python404class MyCustomAuth(httpx.Auth):405 requires_request_body = True406407 def __init__(self, token):408 self.token = token409410 def auth_flow(self, request):411 response = yield request412 if response.status_code == 401:413 # If the server issues a 401 response then resend the request,414 # with a custom `X-Authentication` header.415 request.headers['X-Authentication'] = self.sign_request(...)416 yield request417418 def sign_request(self, request):419 # Create a request signature, based on `request.method`, `request.url`,420 # `request.headers`, and `request.content`.421 ...422```423

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

lines 424–427why, one step back
424## SSL certificates
425426When 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).427

Warn if cert / verify / trust_env are passed to client.request() (#597)Tom Christiebefore adoption1e23855

line 428why, one step back
428### Changing the verification defaults

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

lines 429–431why, one step back
429
430By 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.431

Warn if cert / verify / trust_env are passed to client.request() (#597)Tom Christiebefore adoption1e23855

line 432why, one step back
432If 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 433–436why, one step back
433
434```python435import httpx436

Proposed 0.11 docs (#727)Tom Christiebefore adoption2038919

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

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

line 438why, one step back
438```

Warn if cert / verify / trust_env are passed to client.request() (#597)Tom Christiebefore adoption1e23855

lines 439–444why, one step back
439
440You can also disable the SSL verification...441442```python443import httpx444

Proposed 0.11 docs (#727)Tom Christiebefore adoption2038919

line 445why, one step back
445r = httpx.get("https://example.org", verify=False)

Warn if cert / verify / trust_env are passed to client.request() (#597)Tom Christiebefore adoption1e23855

lines 446–452why, one step back
446```
447448### SSL configuration on client instances449450If you're using a `Client()` instance, then you should pass any SSL settings when instantiating the client.451452```python

Proposed 0.11 docs (#727)Tom Christiebefore adoption2038919

line 453why, one step back
453client = httpx.Client(verify=False)

Warn if cert / verify / trust_env are passed to client.request() (#597)Tom Christiebefore adoption1e23855

lines 454–455why, one step back
454```
455

Update advanced.mdTom Christiebefore adoptionddc4885

line 456why, one step back
456The `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 457–469why, one step back
457
458### Making HTTPS requests to a local server459460When making requests to local servers, such as a development server running on `localhost`, you will typically be using unencrypted HTTP connections.461462If 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:4634641. Use [trustme-cli](https://github.com/sethmlarson/trustme-cli/) to generate a pair of server key/cert files, and a client cert file.4651. 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.)4661. Tell HTTPX to use the certificates stored in `client.pem`:467468```python469>>> import httpx

Proposed 0.11 docs (#727)Tom Christiebefore adoption2038919

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

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

lines 471–473why, one step back
471>>> r
472Response <200 OK>473```