http2.md
This file
Path
docs/http2.md
60 lines · 2.3 KiB · Markdown
Last changed by
Proposed 0.11 docs (#727)Tom Christiebefore adoption2038919
History of this fileTake it
Better HTTP/2 defaults. (#584)Tom Christiebefore adoption30229f1
lines 1–20why, one step back1# HTTP/223HTTP/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/220Proposed 0.11 docs (#727)Tom Christiebefore adoption2038919
line 21why, one step back21The HTTPX client provides HTTP/2 support, **which is currently only available with the async client**.Better HTTP/2 defaults. (#584)Tom Christiebefore adoption30229f1
lines 22–23why, one step back2223HTTP/2 support is not enabled by default, because HTTP/1.1 is a mature,Proposed 0.11 docs (#727)Tom Christiebefore adoption2038919
lines 24–26why, one step back24battle-hardened transport layer, and our HTTP/1.1 may be considered the more robust25option at this point in time. It is possible that a future version of `httpx` may26enable HTTP/2 support by default.Better HTTP/2 defaults. (#584)Tom Christiebefore adoption30229f1
line 27why, one step back27Proposed 0.11 docs (#727)Tom Christiebefore adoption2038919
line 28why, one step back28If you're issuing highly concurrent requests you might want to considerBetter HTTP/2 defaults. (#584)Tom Christiebefore adoption30229f1
lines 29–32why, one step back29trying out our HTTP/2 support. You can do so by instantiating a client with30HTTP/2 support enabled:3132```pythonRename Client to AsyncClient (with compat synonym) (#680)Florimond Mancabefore adoptione284b84
line 33why, one step back33client = httpx.AsyncClient(http2=True)Better HTTP/2 defaults. (#584)Tom Christiebefore adoption30229f1
lines 34–41why, one step back34...35```3637You can also instantiate a client as a context manager, to ensure that all38HTTP connections are nicely scoped, and will be closed once the context block39is exited.4041```pythonRename Client to AsyncClient (with compat synonym) (#680)Florimond Mancabefore adoptione284b84
line 42why, one step back42async with httpx.AsyncClient(http2=True) as client:Better HTTP/2 defaults. (#584)Tom Christiebefore adoption30229f1
lines 43–56why, one step back43 ...44```4546## Inspecting the HTTP version4748Enabling HTTP/2 support on the client does not *necessarily* mean that your49requests and responses will be transported over HTTP/2, since both the client50*and* the server need to support HTTP/2. If you connect to a server that only51supports HTTP/1.1 the client will use a standard HTTP/1.1 connection instead.5253You can determine which version of the HTTP protocol was used by examining54the `.http_version` property on the response.5556```pythonRename Client to AsyncClient (with compat synonym) (#680)Florimond Mancabefore adoptione284b84
line 57why, one step back57client = httpx.AsyncClient(http2=True)Better HTTP/2 defaults. (#584)Tom Christiebefore adoption30229f1
lines 58–60why, one step back58response = await client.get(...)59print(response.http_version) # "HTTP/1.0", "HTTP/1.1", or "HTTP/2".60```