File tree Expand file tree Collapse file tree 1 file changed +12
-3
lines changed
Expand file tree Collapse file tree 1 file changed +12
-3
lines changed Original file line number Diff line number Diff line change @@ -228,10 +228,10 @@ every time a particular type of event takes place.
228228
229229There are currently two event hooks:
230230
231- * ` request ` - Called once a request is about to be sent. Passed the ` request ` instance.
232- * ` response ` - Called once the response has been returned. Passed the ` response ` instance.
231+ * ` request ` - Called after a request is fully prepared, but before it is sent to the network . Passed the ` request ` instance.
232+ * ` response ` - Called after the response has been fetched from the network, but before it is returned to the caller . Passed the ` response ` instance.
233233
234- These allow you to install client-wide functionality such as logging and monitoring.
234+ These allow you to install client-wide functionality such as logging, monitoring or tracing .
235235
236236``` python
237237def log_request (request ):
@@ -255,6 +255,15 @@ def raise_on_4xx_5xx(response):
255255client = httpx.Client(event_hooks = {' response' : [raise_on_4xx_5xx]})
256256```
257257
258+ The hooks are also allowed to modify ` request ` and ` response ` objects.
259+
260+ ``` python
261+ def add_timestamp (request ):
262+ request.headers[' x-request-timestamp' ] = datetime.now(tz = datetime.utc).isoformat()
263+
264+ client = httpx.Client(event_hooks = {' request' : [add_timestamp]})
265+ ```
266+
258267Event hooks must always be set as a ** list of callables** , and you may register
259268multiple event hooks for each type of event.
260269
You can’t perform that action at this time.
0 commit comments