Improving documentation/behavior for event hooks #1637
-
Right now (at least looking at https://www.python-httpx.org/advanced/#event-hooks) it's not really specified whether request/response hooks are allowed to modify the request/response objects, or whether that's actually useful. Specifically, for response objects, it says
Which can be interpreted in a way that does not accurately reflect the current behavior (response being returned from the If being able to modify requests/responses is intentional, it's probably a good idea to make that clear in the documentation and provide some examples. If it's not, then maybe it's better to enforce that in the code. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 5 replies
-
Good question. The request hooks: https://github.com/encode/httpx/blob/master/httpx/_client.py#L902-L903 And response hooks: https://github.com/encode/httpx/blob/master/httpx/_client.py#L882-L883 Both simply pass the request or response instance to the function. To the extent that request/response instances have some mutable aspects, you could alter some bits of them in the function as a side-effect. For instance: def hook(request):
request.headers['X-Custom'] = '...' That's not particularly an intentional design feature, and I have considered if we'd do better with headers being an immutable data structure or not. (Although even if they were, it'd still be fiddly to make the entire request instance properly immutable.) So 🤷♂️ |
Beta Was this translation helpful? Give feedback.
Good question.
The request hooks:
https://github.com/encode/httpx/blob/master/httpx/_client.py#L902-L903
And response hooks:
https://github.com/encode/httpx/blob/master/httpx/_client.py#L882-L883
Both simply pass the request or response instance to the function.
To the extent that request/response instances have some mutable aspects, you could alter some bits of them in the function as a side-effect. For instance:
That's not particularly an intentional design feature, and I have considered if we'd do better with headers being an immutable data structure or not. (Although even if they were, it'd still be fiddly to make the entire r…