Skip to content

Hook System ## Business Purpose

Allow external callbacks to modify or observe the request/response lifecycle, enabling logging, caching, authentication, or custom transformations. ## Current Behaviors - HOOKS is a module-level constant list containing 'response' src/requests/hooks.py:22. - default_hooks() returns a dictionary with 'response' mapped to an empty list src/requests/hooks.py:25. - dispatch_hook() takes a key, hooks dict, and data (Response), iterates over callables, and updates data with return values src/requests/hooks.py:32. - Hooks can be registered via register_hook() on RequestHooksMixin src/requests/models.py:258. - Session hooks are merged with request hooks, with request hooks overriding session hooks for the same event src/requests/sessions.py:76. ## Technical Implementation The send method in Session dispatches the 'response' hook on the response object after it is built src/requests/sessions.py:752. Hooks are retrieved from the prepared request's hooks dict. ## Definition of Done - A hook registered via session.hooks['response'].append(callback) is called for each response. - The callback receives the Response object and can modify it; the modified object is returned. - Request-level hooks override session-level hooks for the same event. - A hook that returns None does not alter the response. - dispatch_hook handles a single callable or a list of callables.