</> Vikas Sharma

Requests Library Deep Dive - 8


Continuing the study of the requests module. Today, I am diving into requests/hooks.py, a lightweight but powerful module that allows users to attach custom logic to the request/response lifecycle.

Key Concepts in requests/hooks.py

Purpose

Hooks provide a way to execute user-defined functions during specific parts of the requests/response lifecycle. This makes it possible to customize or extend the behavior of requests without modifying its core code.

Hook Events

Structure of Hooks

default_hooks: Defines the default structure of hooks.

default_hooks = {'response': []}

Each key corresponds to a specific lifecycle event, and the value is a list of functions to be executed.

Dispatching Hooks

dispatch_hooks: This function executes all functions associated with a specific hook.

def dispatch_hook(key, hooks, hook_data, **kwargs):
    if hooks.get(key):
        for hook in hooks[key]:
            hook_data = hook(hook_data, **kwargs)
    return hook_data

Using Hooks with Response

Hooks are typically used to inspect or modify a Response before it’s returned to the user. For example:

import requests

def response_hook(response, *args, **kwargs):
    print(f'Hook: Status Code - {response.status_code}')
    return response

hooks = {'response': [response_hook]}
response = requests.get('https://example.com', hooks=hooks)