Skip to content
Myr

http2.md

This file

Path

docs/http2.md

68 lines · 2.5 KiB · Markdown

Last changed by

HTTP/2 becomes fully optional (#1140)Tom Christiebefore adoption8d9dfb5

History of this file

Take it

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

Fix repo URLTom Christiebefore adoptiond515e4b

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.

Fix repo URLTom Christiebefore adoptiond515e4b

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

HTTP/2 becomes fully optional (#1140)Tom Christiebefore adoption8d9dfb5

lines 27–28why, one step back
27trying out our HTTP/2 support. You can do so by first making sure to install
28the optional HTTP/2 dependencies...

Fix repo URLTom Christiebefore adoptiond515e4b

line 29why, one step back
29

HTTP/2 becomes fully optional (#1140)Tom Christiebefore adoption8d9dfb5

lines 30–35why, one step back
30```shell
31$ pip install httpx[http2]32```3334And then then instantiating a client with HTTP/2 support enabled:35

Fix repo URLTom Christiebefore adoptiond515e4b

lines 36–48why, one step back
36```python
37client = httpx.AsyncClient(http2=True)38...39```4041You can also instantiate a client as a context manager, to ensure that all42HTTP connections are nicely scoped, and will be closed once the context block43is exited.4445```python46async with httpx.AsyncClient(http2=True) as client:47 ...48```

Update HTTP/2 docsTom Christiebefore adoptionbafa32a

line 49why, one step back
49

HTTP/2 becomes fully optional (#1140)Tom Christiebefore adoption8d9dfb5

line 50why, one step back
50HTTP/2 support is available on both `Client` and `AsyncClient`, although it's

Update HTTP/2 docsTom Christiebefore adoptionbafa32a

lines 51–52why, one step back
51typically more useful in async contexts if you're issuing lots of concurrent
52requests.

Fix repo URLTom Christiebefore adoptiond515e4b

lines 53–68why, one step back
53
54## Inspecting the HTTP version5556Enabling HTTP/2 support on the client does not *necessarily* mean that your57requests and responses will be transported over HTTP/2, since both the client58*and* the server need to support HTTP/2. If you connect to a server that only59supports HTTP/1.1 the client will use a standard HTTP/1.1 connection instead.6061You can determine which version of the HTTP protocol was used by examining62the `.http_version` property on the response.6364```python65client = httpx.AsyncClient(http2=True)66response = await client.get(...)67print(response.http_version) # "HTTP/1.0", "HTTP/1.1", or "HTTP/2".68```