October DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsSlow PC?RecommendedPC slow today? Run a repair scan before it gets worseResolve common Windows issues and optimize system performance.Scan NowOctober DealsAmazon USDeal season is back - check today's better picksAmazon US: current deals, useful picks and tech finds.See Picks×
Skip to content
World desk3 min

Why Python’s urljoin Removes a /v1 API Prefix—and How to Preserve It

A leading slash in the endpoint makes urljoin replace the base path, so /v1 disappears. See the URL resolution rules, correct relative-path pattern, and security checks.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

urljoin 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.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

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:

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.

How should I diagnose a missing prefix in an API client?

  1. Record the configured base URL. Check whether it ends in /v1 or /v1/.
  2. Inspect the exact endpoint string. A leading / makes it root-relative; a relative string such as items is merged with the base path.
  3. Resolve and inspect the final URL. Log or otherwise examine the prepared request URL before sending it, including scheme, authority, path, and query.
  4. 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.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

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.

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.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
PC Slower Than It Used to Be?Free scan - under a minute
Outdated Drivers Are Slowing You DownFree scan - exact matches

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.