PreparedRequest Class — requests Architecture
Architecture documentation for the PreparedRequest class in models.py from the requests codebase.
Entity Profile
Dependency Diagram
graph TD c8cfcd0c_a36a_3124_6cd4_1516b06c63b3["PreparedRequest"] f47304c1_b2c6_e66a_f59e_e2534a06b3c8["RequestEncodingMixin"] c8cfcd0c_a36a_3124_6cd4_1516b06c63b3 -->|extends| f47304c1_b2c6_e66a_f59e_e2534a06b3c8 95bd3ad9_fc53_a808_b595_bd723df9bdcd["RequestHooksMixin"] c8cfcd0c_a36a_3124_6cd4_1516b06c63b3 -->|extends| 95bd3ad9_fc53_a808_b595_bd723df9bdcd 461bc6e0_32e7_8eab_ec87_7226e7be0d13["models.py"] c8cfcd0c_a36a_3124_6cd4_1516b06c63b3 -->|defined in| 461bc6e0_32e7_8eab_ec87_7226e7be0d13 bcd33cc2_9f6b_9812_13af_21b2f3722d2f["__init__()"] c8cfcd0c_a36a_3124_6cd4_1516b06c63b3 -->|method| bcd33cc2_9f6b_9812_13af_21b2f3722d2f 3fc6c343_3d70_670f_636f_1fcab0f8a681["prepare()"] c8cfcd0c_a36a_3124_6cd4_1516b06c63b3 -->|method| 3fc6c343_3d70_670f_636f_1fcab0f8a681 51fd93d4_93f5_a3e3_841a_11cbba2e7fa8["__repr__()"] c8cfcd0c_a36a_3124_6cd4_1516b06c63b3 -->|method| 51fd93d4_93f5_a3e3_841a_11cbba2e7fa8 ff6a6233_f223_912a_095d_8a2f8f7ceeba["copy()"] c8cfcd0c_a36a_3124_6cd4_1516b06c63b3 -->|method| ff6a6233_f223_912a_095d_8a2f8f7ceeba 918832c2_0c83_1556_bca4_78b83a2cc415["prepare_method()"] c8cfcd0c_a36a_3124_6cd4_1516b06c63b3 -->|method| 918832c2_0c83_1556_bca4_78b83a2cc415 f217af3f_cba9_c7bc_ae2a_4fe5a6bea032["_get_idna_encoded_host()"] c8cfcd0c_a36a_3124_6cd4_1516b06c63b3 -->|method| f217af3f_cba9_c7bc_ae2a_4fe5a6bea032 77892816_404a_4492_b558_b565f63d2ad0["prepare_url()"] c8cfcd0c_a36a_3124_6cd4_1516b06c63b3 -->|method| 77892816_404a_4492_b558_b565f63d2ad0 cf59faf9_2b09_589c_b9c7_f2e4c7d65c4d["prepare_headers()"] c8cfcd0c_a36a_3124_6cd4_1516b06c63b3 -->|method| cf59faf9_2b09_589c_b9c7_f2e4c7d65c4d fdb744d2_4cc0_a0f9_c288_a0abeac5e58e["prepare_body()"] c8cfcd0c_a36a_3124_6cd4_1516b06c63b3 -->|method| fdb744d2_4cc0_a0f9_c288_a0abeac5e58e ad57bfe9_2bba_ea6e_5e85_44c51303ed35["prepare_content_length()"] c8cfcd0c_a36a_3124_6cd4_1516b06c63b3 -->|method| ad57bfe9_2bba_ea6e_5e85_44c51303ed35 bea876a9_383a_fe0e_3e83_080ef6697c9f["prepare_auth()"] c8cfcd0c_a36a_3124_6cd4_1516b06c63b3 -->|method| bea876a9_383a_fe0e_3e83_080ef6697c9f
Relationship Graph
Source Code
src/requests/models.py lines 315–639
class PreparedRequest(RequestEncodingMixin, RequestHooksMixin):
"""The fully mutable :class:`PreparedRequest <PreparedRequest>` object,
containing the exact bytes that will be sent to the server.
Instances are generated from a :class:`Request <Request>` object, and
should not be instantiated manually; doing so may produce undesirable
effects.
Usage::
>>> import requests
>>> req = requests.Request('GET', 'https://httpbin.org/get')
>>> r = req.prepare()
>>> r
<PreparedRequest [GET]>
>>> s = requests.Session()
>>> s.send(r)
<Response [200]>
"""
def __init__(self):
#: HTTP verb to send to the server.
self.method = None
#: HTTP URL to send the request to.
self.url = None
#: dictionary of HTTP headers.
self.headers = None
# The `CookieJar` used to create the Cookie header will be stored here
# after prepare_cookies is called
self._cookies = None
#: request body to send to the server.
self.body = None
#: dictionary of callback hooks, for internal usage.
self.hooks = default_hooks()
#: integer denoting starting position of a readable file-like body.
self._body_position = None
def prepare(
self,
method=None,
url=None,
headers=None,
files=None,
data=None,
params=None,
auth=None,
cookies=None,
hooks=None,
json=None,
):
"""Prepares the entire request with the given parameters."""
self.prepare_method(method)
self.prepare_url(url, params)
self.prepare_headers(headers)
self.prepare_cookies(cookies)
self.prepare_body(data, files, json)
self.prepare_auth(auth, url)
# Note that prepare_auth must be last to enable authentication schemes
# such as OAuth to work on a fully prepared request.
# This MUST go after prepare_auth. Authenticators could add a hook
self.prepare_hooks(hooks)
def __repr__(self):
return f"<PreparedRequest [{self.method}]>"
def copy(self):
p = PreparedRequest()
p.method = self.method
p.url = self.url
p.headers = self.headers.copy() if self.headers is not None else None
p._cookies = _copy_cookie_jar(self._cookies)
p.body = self.body
p.hooks = self.hooks
p._body_position = self._body_position
return p
def prepare_method(self, method):
Domain
Defined In
Source
Frequently Asked Questions
What is the PreparedRequest class?
PreparedRequest is a class in the requests codebase, defined in src/requests/models.py.
Where is PreparedRequest defined?
PreparedRequest is defined in src/requests/models.py at line 315.
What does PreparedRequest extend?
PreparedRequest extends RequestEncodingMixin, RequestHooksMixin.
Analyze Your Own Codebase
Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.
Try Supermodel Free