prepare_url() — requests Function Reference
Architecture documentation for the prepare_url() function in models.py from the requests codebase.
Entity Profile
Dependency Diagram
graph TD 77892816_404a_4492_b558_b565f63d2ad0["prepare_url()"] c8cfcd0c_a36a_3124_6cd4_1516b06c63b3["PreparedRequest"] 77892816_404a_4492_b558_b565f63d2ad0 -->|defined in| c8cfcd0c_a36a_3124_6cd4_1516b06c63b3 3fc6c343_3d70_670f_636f_1fcab0f8a681["prepare()"] 3fc6c343_3d70_670f_636f_1fcab0f8a681 -->|calls| 77892816_404a_4492_b558_b565f63d2ad0 f217af3f_cba9_c7bc_ae2a_4fe5a6bea032["_get_idna_encoded_host()"] 77892816_404a_4492_b558_b565f63d2ad0 -->|calls| f217af3f_cba9_c7bc_ae2a_4fe5a6bea032 05aa4b39_ee11_5653_43fd_4e9fe439edad["_encode_params()"] 77892816_404a_4492_b558_b565f63d2ad0 -->|calls| 05aa4b39_ee11_5653_43fd_4e9fe439edad 388e0e62_627f_7960_2007_7582a48924d4["unicode_is_ascii()"] 77892816_404a_4492_b558_b565f63d2ad0 -->|calls| 388e0e62_627f_7960_2007_7582a48924d4 0ab29509_59a1_1f68_fae2_146376240019["to_native_string()"] 77892816_404a_4492_b558_b565f63d2ad0 -->|calls| 0ab29509_59a1_1f68_fae2_146376240019 f46aaada_a38d_85e6_6125_21764bf29eda["requote_uri()"] 77892816_404a_4492_b558_b565f63d2ad0 -->|calls| f46aaada_a38d_85e6_6125_21764bf29eda style 77892816_404a_4492_b558_b565f63d2ad0 fill:#6366f1,stroke:#818cf8,color:#fff
Relationship Graph
Source Code
src/requests/models.py lines 411–483
def prepare_url(self, url, params):
"""Prepares the given HTTP URL."""
#: Accept objects that have string representations.
#: We're unable to blindly call unicode/str functions
#: as this will include the bytestring indicator (b'')
#: on python 3.x.
#: https://github.com/psf/requests/pull/2238
if isinstance(url, bytes):
url = url.decode("utf8")
else:
url = str(url)
# Remove leading whitespaces from url
url = url.lstrip()
# Don't do any URL preparation for non-HTTP schemes like `mailto`,
# `data` etc to work around exceptions from `url_parse`, which
# handles RFC 3986 only.
if ":" in url and not url.lower().startswith("http"):
self.url = url
return
# Support for unicode domain names and paths.
try:
scheme, auth, host, port, path, query, fragment = parse_url(url)
except LocationParseError as e:
raise InvalidURL(*e.args)
if not scheme:
raise MissingSchema(
f"Invalid URL {url!r}: No scheme supplied. "
f"Perhaps you meant https://{url}?"
)
if not host:
raise InvalidURL(f"Invalid URL {url!r}: No host supplied")
# In general, we want to try IDNA encoding the hostname if the string contains
# non-ASCII characters. This allows users to automatically get the correct IDNA
# behaviour. For strings containing only ASCII characters, we need to also verify
# it doesn't start with a wildcard (*), before allowing the unencoded hostname.
if not unicode_is_ascii(host):
try:
host = self._get_idna_encoded_host(host)
except UnicodeError:
raise InvalidURL("URL has an invalid label.")
elif host.startswith(("*", ".")):
raise InvalidURL("URL has an invalid label.")
# Carefully reconstruct the network location
netloc = auth or ""
if netloc:
netloc += "@"
netloc += host
if port:
netloc += f":{port}"
# Bare domains aren't valid URLs.
if not path:
path = "/"
if isinstance(params, (str, bytes)):
params = to_native_string(params)
enc_params = self._encode_params(params)
if enc_params:
if query:
query = f"{query}&{enc_params}"
else:
query = enc_params
url = requote_uri(urlunparse([scheme, netloc, path, None, query, fragment]))
self.url = url
Domain
Subdomains
Defined In
Called By
Source
Frequently Asked Questions
What does prepare_url() do?
prepare_url() is a function in the requests codebase, defined in src/requests/models.py.
Where is prepare_url() defined?
prepare_url() is defined in src/requests/models.py at line 411.
What does prepare_url() call?
prepare_url() calls 5 function(s): _encode_params, _get_idna_encoded_host, requote_uri, to_native_string, unicode_is_ascii.
What calls prepare_url()?
prepare_url() 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