extensions.md
This file
Path
docs/advanced/extensions.md
201 lines · 7.9 KiB · Markdown
Last changed by
Extensions docs (#3080)Tom Christiebefore adoptionc51af4b
History of this fileTake it
Extensions docs (#3080)Tom Christiebefore adoptionc51af4b
lines 1–201why, one step back1# Extensions23Request and response extensions provide a untyped space where additional information may be added.45Extensions should be used for features that may not be available on all transports, and that do not fit neatly into [the simplified request/response model](https://www.encode.io/httpcore/extensions/) that the underlying `httpcore` pacakge uses as it's API.67Several extensions are supported on the request:89```python10# Request timeouts actually implemented as an extension on11# the request, ensuring that they are passed throughout the12# entire call stack.13client = httpx.Client()14response = client.get(15 "https://www.example.com",16 extensions={"timeout": {"connect": 5.0}}17)18response.request.extensions["timeout"]19{"connect": 5.0}20```2122And on the response:2324```python25client = httpx.Client()26response = client.get("https://www.example.com")27print(response.extensions["http_version"]) # b"HTTP/1.1"28# Other server responses could have been29# b"HTTP/0.9", b"HTTP/1.0", or b"HTTP/1.1"30```3132## Request Extensions3334### `"trace"`3536The trace extension allows a callback handler to be installed to monitor the internal37flow of events within the underlying `httpcore` transport.3839The simplest way to explain this is with an example:4041```python42import httpx4344def log(event_name, info):45 print(event_name, info)4647client = httpx.Client()48response = client.get("https://www.example.com/", extensions={"trace": log})49# connection.connect_tcp.started {'host': 'www.example.com', 'port': 443, 'local_address': None, 'timeout': None}50# connection.connect_tcp.complete {'return_value': <httpcore.backends.sync.SyncStream object at 0x1093f94d0>}51# connection.start_tls.started {'ssl_context': <ssl.SSLContext object at 0x1093ee750>, 'server_hostname': b'www.example.com', 'timeout': None}52# connection.start_tls.complete {'return_value': <httpcore.backends.sync.SyncStream object at 0x1093f9450>}53# http11.send_request_headers.started {'request': <Request [b'GET']>}54# http11.send_request_headers.complete {'return_value': None}55# http11.send_request_body.started {'request': <Request [b'GET']>}56# http11.send_request_body.complete {'return_value': None}57# http11.receive_response_headers.started {'request': <Request [b'GET']>}58# http11.receive_response_headers.complete {'return_value': (b'HTTP/1.1', 200, b'OK', [(b'Age', b'553715'), (b'Cache-Control', b'max-age=604800'), (b'Content-Type', b'text/html; charset=UTF-8'), (b'Date', b'Thu, 21 Oct 2021 17:08:42 GMT'), (b'Etag', b'"3147526947+ident"'), (b'Expires', b'Thu, 28 Oct 2021 17:08:42 GMT'), (b'Last-Modified', b'Thu, 17 Oct 2019 07:18:26 GMT'), (b'Server', b'ECS (nyb/1DCD)'), (b'Vary', b'Accept-Encoding'), (b'X-Cache', b'HIT'), (b'Content-Length', b'1256')])}59# http11.receive_response_body.started {'request': <Request [b'GET']>}60# http11.receive_response_body.complete {'return_value': None}61# http11.response_closed.started {}62# http11.response_closed.complete {'return_value': None}63```6465The `event_name` and `info` arguments here will be one of the following:6667* `{event_type}.{event_name}.started`, `<dictionary of keyword arguments>`68* `{event_type}.{event_name}.complete`, `{"return_value": <...>}`69* `{event_type}.{event_name}.failed`, `{"exception": <...>}`7071Note that when using async code the handler function passed to `"trace"` must be an `async def ...` function.7273The following event types are currently exposed...7475**Establishing the connection**7677* `"connection.connect_tcp"`78* `"connection.connect_unix_socket"`79* `"connection.start_tls"`8081**HTTP/1.1 events**8283* `"http11.send_request_headers"`84* `"http11.send_request_body"`85* `"http11.receive_response"`86* `"http11.receive_response_body"`87* `"http11.response_closed"`8889**HTTP/2 events**9091* `"http2.send_connection_init"`92* `"http2.send_request_headers"`93* `"http2.send_request_body"`94* `"http2.receive_response_headers"`95* `"http2.receive_response_body"`96* `"http2.response_closed"`9798The exact set of trace events may be subject to change across different versions of `httpcore`. If you need to rely on a particular set of events it is recommended that you pin installation of the package to a fixed version.99100### `"sni_hostname"`101102The server's hostname, which is used to confirm the hostname supplied by the SSL certificate.103104If you want to connect to an explicit IP address rather than using the standard DNS hostname lookup, then you'll need to use this request extension.105106For example:107108``` python109# Connect to '185.199.108.153' but use 'www.encode.io' in the Host header,110# and use 'www.encode.io' when SSL verifying the server hostname.111client = httpx.Client()112headers = {"Host": "www.encode.io"}113extensions = {"sni_hostname": "www.encode.io"}114response = client.get(115 "https://185.199.108.153/path",116 headers=headers,117 extensions=extensions118)119```120121### `"timeout"`122123A dictionary of `str: Optional[float]` timeout values.124125May include values for `'connect'`, `'read'`, `'write'`, or `'pool'`.126127For example:128129```python130# Timeout if a connection takes more than 5 seconds to established, or if131# we are blocked waiting on the connection pool for more than 10 seconds.132client = httpx.Client()133response = client.get(134 "https://www.example.com",135 extensions={"timeout": {"connect": 5.0, "pool": 10.0}}136)137```138139This extension is how the `httpx` timeouts are implemented, ensuring that the timeout values are associated with the request instance and passed throughout the stack. You shouldn't typically be working with this extension directly, but use the higher level `timeout` API instead.140141## Response Extensions142143### `"http_version"`144145The HTTP version, as bytes. Eg. `b"HTTP/1.1"`.146147When using HTTP/1.1 the response line includes an explicit version, and the value of this key could feasibly be one of `b"HTTP/0.9"`, `b"HTTP/1.0"`, or `b"HTTP/1.1"`.148149When using HTTP/2 there is no further response versioning included in the protocol, and the value of this key will always be `b"HTTP/2"`.150151### `"reason_phrase"`152153The reason-phrase of the HTTP response, as bytes. For example `b"OK"`. Some servers may include a custom reason phrase, although this is not recommended.154155HTTP/2 onwards does not include a reason phrase on the wire.156157When no key is included, a default based on the status code may be used.158159### `"stream_id"`160161When HTTP/2 is being used the `"stream_id"` response extension can be accessed to determine the ID of the data stream that the response was sent on.162163### `"network_stream"`164165The `"network_stream"` extension allows developers to handle HTTP `CONNECT` and `Upgrade` requests, by providing an API that steps outside the standard request/response model, and can directly read or write to the network.166167The interface provided by the network stream:168169* `read(max_bytes, timeout = None) -> bytes`170* `write(buffer, timeout = None)`171* `close()`172* `start_tls(ssl_context, server_hostname = None, timeout = None) -> NetworkStream`173* `get_extra_info(info) -> Any`174175This API can be used as the foundation for working with HTTP proxies, WebSocket upgrades, and other advanced use-cases.176177See the [network backends documentation](https://www.encode.io/httpcore/network-backends/) for more information on working directly with network streams.178179**Extra network information**180181The network stream abstraction also allows access to various low-level information that may be exposed by the underlying socket:182183```python184response = httpx.get("https://www.example.com")185network_stream = response.extensions["network_stream"]186187client_addr = network_stream.get_extra_info("client_addr")188server_addr = network_stream.get_extra_info("server_addr")189print("Client address", client_addr)190print("Server address", server_addr)191```192193The socket SSL information is also available through this interface, although you need to ensure that the underlying connection is still open, in order to access it...194195```python196with httpx.stream("GET", "https://www.example.com") as response:197 network_stream = response.extensions["network_stream"]198199 ssl_object = network_stream.get_extra_info("ssl_object")200 print("TLS version", ssl_object.version())201```