{"record":{"id":"e8fe13b5afb24b50","repo":"psf/requests","slug":"invalid-url-url-r-no-host-supplied","errorCode":null,"errorMessage":"Invalid URL {url!r}: No host supplied","messagePattern":"Invalid URL (.+?): No host supplied","errorType":"exception","errorClass":"InvalidURL","httpStatus":null,"severity":"error","filePath":"src/requests/models.py","lineNumber":522,"sourceCode":"        # handles RFC 3986 only.\n        if \":\" in url and not url.lower().startswith(\"http\"):\n            self.url = url\n            return\n\n        # Support for unicode domain names and paths.\n        try:\n            scheme, auth, host, port, path, query, fragment = parse_url(url)\n        except LocationParseError as e:\n            raise InvalidURL(*e.args)\n\n        if not scheme:\n            raise MissingSchema(\n                f\"Invalid URL {url!r}: No scheme supplied. \"\n                f\"Perhaps you meant https://{url}?\"\n            )\n\n        if not host:\n            raise InvalidURL(f\"Invalid URL {url!r}: No host supplied\")\n\n        # In general, we want to try IDNA encoding the hostname if the string contains\n        # non-ASCII characters. This allows users to automatically get the correct IDNA\n        # behaviour. For strings containing only ASCII characters, we need to also verify\n        # it doesn't start with a wildcard (*), before allowing the unencoded hostname.\n        if not unicode_is_ascii(host):\n            try:\n                host = self._get_idna_encoded_host(host)\n            except UnicodeError:\n                raise InvalidURL(\"URL has an invalid label.\")\n        elif host.startswith((\"*\", \".\")):\n            raise InvalidURL(\"URL has an invalid label.\")\n\n        # Carefully reconstruct the network location\n        netloc = auth or \"\"\n        if netloc:\n            netloc += \"@\"\n        netloc += host","sourceCodeStart":504,"sourceCodeEnd":540,"githubUrl":"https://github.com/psf/requests/blob/8068356288978c4f54661ae6f95afe0e0831885e/src/requests/models.py#L504-L540","documentation":"Raised by PreparedRequest.prepare_url when the URL parses with a scheme but no host component — e.g. 'https:///path' (triple slash), 'https://:443/path', or scheme-only strings like 'file:///'. The host is mandatory for HTTP(S) routing, so requests refuses. The message echoes the offending URL for diagnosis.","triggerScenarios":"Triple-slash URLs from buggy templating ('https:///' + host where host is empty); URLs like 'https://:8080' with no hostname; schemes that urllib3 accepts but which carry no authority (e.g. some custom schemes).","commonSituations":"Building URLs from optional config fields that were left blank; service-discovery bugs returning empty hostnames; misconfigured environment variables (BASE_URL='https://').","solutions":["Ensure the host/authority segment is populated before assembling the URL.","Validate with urllib.parse.urlparse(result).netloc before passing to requests.","Fail fast on empty host config at app startup rather than per-request."],"exampleFix":"// before\nurl = f'{scheme}:///{path}'  # triple slash, empty host\nrequests.get(url)\n\n// after\nassert host, 'host must be set'\nurl = f'{scheme}://{host}/{path}'\nrequests.get(url)","handlingStrategy":"validation","validationCode":"from urllib.parse import urlparse\n\ndef has_host(url: str) -> bool:\n    return bool(urlparse(url).netloc)","typeGuard":"from urllib.parse import urlparse\n\ndef is_valid_http_url(url: str) -> bool:\n    p = urlparse(url)\n    return p.scheme in ('http', 'https') and bool(p.hostname)","tryCatchPattern":"from requests.exceptions import InvalidURL\n\ntry:\n    resp = requests.get(url)\nexcept InvalidURL as e:\n    if 'No host supplied' in str(e):\n        # fix and retry\n        ...\n    raise","preventionTips":["Ensure the host/authority segment is populated when assembling URLs.","Validate urlparse(result).netloc before calling requests.","Fail fast on empty host config at startup."],"tags":["url","invalidurl","host","validation","http"],"backgroundTag":null,"analyzedSha":"8068356288978c4f54661ae6f95afe0e0831885e","analyzedAt":"2026-08-11T20:11:09.238Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}