Skip to content
Myr

http2.md

This file

Path

docs/http2.md

62 lines · 2.4 KiB · Markdown

Last changed by

Update HTTP/2 docsTom Christiebefore adoptionbafa32a

History of this file

Take it

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

Rename http2 switch (#586)Tom Christiebefore adoption5076952

lines 1–20why, one step back
1# HTTP/2
23HTTP/2 is a major new iteration of the HTTP protocol, that provides a far more4efficient transport, with potential performance benefits. HTTP/2 does not change5the core semantics of the request or response, but alters the way that data is6sent to and from the server.78Rather that the text format that HTTP/1.1 uses, HTTP/2 is a binary format.9The binary format provides full request and response multiplexing, and efficient10compression of HTTP headers. The stream multiplexing means that where HTTP/1.111requires one TCP stream for each concurrent request, HTTP/2 allows a single TCP12stream to handle multiple concurrent requests.1314HTTP/2 also provides support for functionality such as response prioritization,15and server push.1617For a comprehensive guide to HTTP/2 you may want to check out "[HTTP2 Explained](https://http2-explained.haxx.se/content/en/)".1819## Enabling HTTP/220

Update HTTP/2 docsTom Christiebefore adoptionbafa32a

lines 21–24why, one step back
21When using the `httpx` client, HTTP/2 support is not enabled by default, because
22HTTP/1.1 is a mature, battle-hardened transport layer, and our HTTP/1.123implementation may be considered the more robust option at this point in time.24It is possible that a future version of `httpx` may enable HTTP/2 support by default.

Rename http2 switch (#586)Tom Christiebefore adoption5076952

line 25why, one step back
25

Proposed 0.11 docs (#727)Tom Christiebefore adoption2038919

line 26why, one step back
26If you're issuing highly concurrent requests you might want to consider

Rename http2 switch (#586)Tom Christiebefore adoption5076952

lines 27–30why, one step back
27trying out our HTTP/2 support. You can do so by instantiating a client with
28HTTP/2 support enabled:2930```python

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

line 31why, one step back
31client = httpx.AsyncClient(http2=True)

Rename http2 switch (#586)Tom Christiebefore adoption5076952

lines 32–39why, one step back
32...
33```3435You can also instantiate a client as a context manager, to ensure that all36HTTP connections are nicely scoped, and will be closed once the context block37is exited.3839```python

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

line 40why, one step back
40async with httpx.AsyncClient(http2=True) as client:

Rename http2 switch (#586)Tom Christiebefore adoption5076952

lines 41–42why, one step back
41 ...
42```

Update HTTP/2 docsTom Christiebefore adoptionbafa32a

lines 43–46why, one step back
43
44HTTP/2 support is available on both `Client`, and `AsyncClient`, although it's45typically more useful in async contexts if you're issuing lots of concurrent46requests.

Rename http2 switch (#586)Tom Christiebefore adoption5076952

lines 47–58why, one step back
47
48## Inspecting the HTTP version4950Enabling HTTP/2 support on the client does not *necessarily* mean that your51requests and responses will be transported over HTTP/2, since both the client52*and* the server need to support HTTP/2. If you connect to a server that only53supports HTTP/1.1 the client will use a standard HTTP/1.1 connection instead.5455You can determine which version of the HTTP protocol was used by examining56the `.http_version` property on the response.5758```python

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

line 59why, one step back
59client = httpx.AsyncClient(http2=True)

Rename http2 switch (#586)Tom Christiebefore adoption5076952

lines 60–62why, one step back
60response = await client.get(...)
61print(response.http_version) # "HTTP/1.0", "HTTP/1.1", or "HTTP/2".62```