</> Vikas Sharma

Requests Library Deep Dive - 4


Continuing to study the requests module. Today, trying to go through requests/models.py, which defines the foundational classes for representing HTTP requests and responses.

Key Concepts in requests/models.py

Core Classes

Two critical classes in this module are: Request: Represents an HTTP request. Response: Represents an HTTP response.

  1. Request Class The Request class encapsulates all the data needed to prepare and send an HTTP request.

Key Attributes:

def __init__(self, method, url, headers=None, files=None, data=None, params=None, json=None, ...):
    self.method = method.upper()
    self.url = url
    self.headers = headers or {}
    self.params = params or {}
    self.data = data
    self.json = json
    ...

Preparing a Request:

The prepare method converts the Request into a PreparedRequest object, which is a fully-formed request ready to be sent.

def prepare(self):
    return PreparedRequest().prepare(
        method=self.method,
        url=self.url,
        headers=self.headers,
        files=self.files,
        data=self.data,
        params=self.params,
        json=self.json,
    )
  1. Response Class The Response class holds the result of an HTTP request.

Key Attributes:

def __init__(self):
    self._content = False
    self.status_code = None
    self.headers = CaseInsensitiveDict()
    self.url = None
    self.reason = None
    self.elapsed = None
    self.history = []

Methods:

def raise_for_status(self):
    if 400 <= self.status_code < 600:
        raise HTTPError(f'{self.status_code} Error: {self.reason}')
def json(self, **kwargs):
    return json.loads(self.text, **kwargs)

Interaction Between Request and Response

A Request object is prepared using prepare, resulting in a PreparedRequest. The Session sends the PreparedRequest and receives a Response object. The Response contains all the information about the server’s reply to the request. This module is where a lot of abstraction occurs, making HTTP requests intuitive for end users.