Skip to content

Commit 776dbb5

Browse files
authored
docs: slightly clarify event hooks (#1645)
* Be more specific about when the hooks are called * Explicitly mention and demonstrate that hooks are allowed to modify requests See #1637
1 parent 9b17671 commit 776dbb5

File tree

1 file changed

+12
-3
lines changed

1 file changed

+12
-3
lines changed

docs/advanced.md

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -228,10 +228,10 @@ every time a particular type of event takes place.
228228

229229
There 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
237237
def log_request(request):
@@ -255,6 +255,15 @@ def raise_on_4xx_5xx(response):
255255
client = 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+
258267
Event hooks must always be set as a **list of callables**, and you may register
259268
multiple event hooks for each type of event.
260269

0 commit comments

Comments
 (0)