prepare_body() — requests Function Reference
Architecture documentation for the prepare_body() function in models.py from the requests codebase.
Entity Profile
Dependency Diagram
graph TD fdb744d2_4cc0_a0f9_c288_a0abeac5e58e["prepare_body()"] c8cfcd0c_a36a_3124_6cd4_1516b06c63b3["PreparedRequest"] fdb744d2_4cc0_a0f9_c288_a0abeac5e58e -->|defined in| c8cfcd0c_a36a_3124_6cd4_1516b06c63b3 3fc6c343_3d70_670f_636f_1fcab0f8a681["prepare()"] 3fc6c343_3d70_670f_636f_1fcab0f8a681 -->|calls| fdb744d2_4cc0_a0f9_c288_a0abeac5e58e 8078fe73_7320_a15b_369b_220683f56710["_encode_files()"] fdb744d2_4cc0_a0f9_c288_a0abeac5e58e -->|calls| 8078fe73_7320_a15b_369b_220683f56710 05aa4b39_ee11_5653_43fd_4e9fe439edad["_encode_params()"] fdb744d2_4cc0_a0f9_c288_a0abeac5e58e -->|calls| 05aa4b39_ee11_5653_43fd_4e9fe439edad ad57bfe9_2bba_ea6e_5e85_44c51303ed35["prepare_content_length()"] fdb744d2_4cc0_a0f9_c288_a0abeac5e58e -->|calls| ad57bfe9_2bba_ea6e_5e85_44c51303ed35 afb5f427_a5f4_14c6_bceb_0bab74c9174b["super_len()"] fdb744d2_4cc0_a0f9_c288_a0abeac5e58e -->|calls| afb5f427_a5f4_14c6_bceb_0bab74c9174b style fdb744d2_4cc0_a0f9_c288_a0abeac5e58e fill:#6366f1,stroke:#818cf8,color:#fff
Relationship Graph
Source Code
src/requests/models.py lines 496–572
def prepare_body(self, data, files, json=None):
"""Prepares the given HTTP body data."""
# Check if file, fo, generator, iterator.
# If not, run through normal process.
# Nottin' on you.
body = None
content_type = None
if not data and json is not None:
# urllib3 requires a bytes-like body. Python 2's json.dumps
# provides this natively, but Python 3 gives a Unicode string.
content_type = "application/json"
try:
body = complexjson.dumps(json, allow_nan=False)
except ValueError as ve:
raise InvalidJSONError(ve, request=self)
if not isinstance(body, bytes):
body = body.encode("utf-8")
is_stream = all(
[
hasattr(data, "__iter__"),
not isinstance(data, (basestring, list, tuple, Mapping)),
]
)
if is_stream:
try:
length = super_len(data)
except (TypeError, AttributeError, UnsupportedOperation):
length = None
body = data
if getattr(body, "tell", None) is not None:
# Record the current file position before reading.
# This will allow us to rewind a file in the event
# of a redirect.
try:
self._body_position = body.tell()
except OSError:
# This differentiates from None, allowing us to catch
# a failed `tell()` later when trying to rewind the body
self._body_position = object()
if files:
raise NotImplementedError(
"Streamed bodies and files are mutually exclusive."
)
if length:
self.headers["Content-Length"] = builtin_str(length)
else:
self.headers["Transfer-Encoding"] = "chunked"
else:
# Multi-part file uploads.
if files:
(body, content_type) = self._encode_files(files, data)
else:
if data:
body = self._encode_params(data)
if isinstance(data, basestring) or hasattr(data, "read"):
content_type = None
else:
content_type = "application/x-www-form-urlencoded"
self.prepare_content_length(body)
# Add content-type if it wasn't explicitly provided.
if content_type and ("content-type" not in self.headers):
self.headers["Content-Type"] = content_type
self.body = body
Domain
Subdomains
Defined In
Called By
Source
Frequently Asked Questions
What does prepare_body() do?
prepare_body() is a function in the requests codebase, defined in src/requests/models.py.
Where is prepare_body() defined?
prepare_body() is defined in src/requests/models.py at line 496.
What does prepare_body() call?
prepare_body() calls 4 function(s): _encode_files, _encode_params, prepare_content_length, super_len.
What calls prepare_body()?
prepare_body() is called by 1 function(s): prepare.
Analyze Your Own Codebase
Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.
Try Supermodel Free