Do these 3 things before closing this tab:
1Clear out junk files and repair common Windows errors2Scan for outdated or missing drivers - takes under a minute3Repair Windows errors before they cause bigger problemsurljoin removes a /v1 prefix when the endpoint begins with /: that leading slash makes it an absolute-path reference, so the endpoint path replaces the base URL’s path. For example, joining https://api.example.test/v1 with /items resolves to https://api.example.test/items. To preserve the prefix, use a relative endpoint such as items with a base ending in /, or build and verify the complete endpoint path deliberately.
Why does urljoin remove my /v1 API prefix?
Python’s urllib.parse.urljoin(base, url) resolves the second argument as a URL reference against the first. Under the URL resolution rules documented by Python’s urllib.parse documentation and RFC 3986, a reference beginning with one slash is an absolute-path reference. It replaces the base path rather than being appended to it.
As an Amazon Associate I earn from qualifying purchases.
from urllib.parse import urljoin
base = "https://api.example.test/v1"
urljoin(base, "/items") # https://api.example.test/items
The host and scheme remain, but the resulting path is /items, not /v1/items. Python’s documentation says its urljoin behavior was updated in Python 3.5 to match RFC 3986 semantics.
How do I preserve the base path?
Use a relative endpoint and make the base path’s trailing-slash structure explicit. With /v1/ as the base path, the relative reference items resolves beneath it:
#1 Best Overall
base = "https://api.example.test/v1/"
urljoin(base, "items") # https://api.example.test/v1/items
The trailing slash matters. In URL resolution, a base path without a trailing slash is treated as ending in its final path segment, which a relative reference can replace. A leading slash on the endpoint has the opposite effect: it starts a new path at the authority’s root, regardless of whether the base ends in a slash.
| Base URL | Endpoint reference | Resolved URL | Effect |
|---|---|---|---|
https://api.example.test/v1 |
/items |
https://api.example.test/items |
Leading slash replaces the base path. |
https://api.example.test/v1/ |
items |
https://api.example.test/v1/items |
Relative endpoint is merged beneath the base path. |
https://api.example.test/v1 |
items |
https://api.example.test/items |
Without the trailing slash, the final base segment is replaced. |
These results follow the documented resolution rules; they do not establish the exact inputs behind the incident described in the headline. If the version prefix must be present in every request, another clear design is to include it explicitly in each endpoint and verify the completed URL before sending.
Rank #2
How should I diagnose a missing prefix in an API client?
- Record the configured base URL. Check whether it ends in
/v1or/v1/. - Inspect the exact endpoint string. A leading
/makes it root-relative; a relative string such asitemsis merged with the base path. - Resolve and inspect the final URL. Log or otherwise examine the prepared request URL before sending it, including scheme, authority, path, and query.
- Check the final request against the intended endpoint. Confirm that the path contains the expected version prefix and that query parameters remain correct.
The title’s reported duration and the specific incident inputs, Python deployment version, and request library are not established by Python’s documentation or RFC 3986. A leading slash is a common explanation for a lost prefix, but confirming the cause in a particular failure requires the actual base URL, endpoint string, and final request URL.
Free tools Windows power users keep installed
One-click scans. No signup required.
Can urljoin change the request host too?
Yes. Python warns that an absolute second argument can replace the base URL’s scheme or hostname, not just its path. The Python Software Foundation’s Python 3.14.7 documentation states: “Because an absolute URL may be passed as the url parameter, it is generally not secure to use urljoin with an attacker-controlled url.”
If the endpoint may come from an untrusted source, validate the resolved scheme, authority, and path against what your client is allowed to contact before making the request. Preserving a path prefix alone is not a sufficient destination check.
Quick Recap
Best Value
Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.




