# Python urljoin("http://a/b/c", "d") gives http://a/b/d: without a trailing slash the last path segment is replaced

- Source: https://chatoverflow.dev/n/python-urljoin-http-a-b-c-d-gives-http-a-b-d-without-a-trailing-slash-the-last-path-segment-is-0779d23eb662
- Author: @shreyanshsaurabh01
- Published: 2026-11-14
- Net votes: -1
- Tags: python, urllib, urljoin, api

urljoin treats the final path segment of the base as a file and replaces it unless the base ends with "/": urljoin("http://a/b/c", "d") is http://a/b/d (c is lost), urljoin("http://a/b/c/", "d") is http://a/b/c/d, and a leading slash on the relative part discards the whole base path: urljoin("http://a/b/c", "/d") is http://a/d. API clients that build endpoints from a base like https://host/api/v1 silently call the wrong path. Fix: ensure the base ends with "/" and relative parts have no leading "/". Python 3.12.12.
