{"id":"825b20b7267f0bd1","repo":"psf/requests","slug":"please-check-proxy-url-it-is-malformed-and-could","errorCode":null,"errorMessage":"Please check proxy URL. It is malformed and could be missing the host.","messagePattern":"Please check proxy URL\\. It is malformed and could be missing the host\\.","errorType":"exception","errorClass":"InvalidProxyURL","httpStatus":null,"severity":"error","filePath":"src/requests/adapters.py","lineNumber":496,"sourceCode":"        :rtype:\n            urllib3.HTTPConnectionPool\n        \"\"\"\n        assert _is_prepared(request)\n\n        proxy = select_proxy(request.url, proxies)\n        try:\n            host_params, pool_kwargs = self.build_connection_pool_key_attributes(\n                request,\n                verify,\n                cert,\n            )\n        except ValueError as e:\n            raise InvalidURL(e, request=request)\n        if proxy:\n            proxy = prepend_scheme_if_needed(proxy, \"http\")\n            proxy_url = parse_url(proxy)\n            if not proxy_url.host:\n                raise InvalidProxyURL(\n                    \"Please check proxy URL. It is malformed \"\n                    \"and could be missing the host.\"\n                )\n            proxy_manager = self.proxy_manager_for(proxy)\n            conn = proxy_manager.connection_from_host(\n                **host_params, pool_kwargs=pool_kwargs\n            )\n        else:\n            # Only scheme should be lower case\n            conn = self.poolmanager.connection_from_host(\n                **host_params, pool_kwargs=pool_kwargs\n            )\n\n        return conn\n\n    def get_connection(\n        self, url: str, proxies: dict[str, str] | None = None\n    ) -> HTTPConnectionPool:","sourceCodeStart":478,"sourceCodeEnd":514,"githubUrl":"https://github.com/psf/requests/blob/414f0513c33883adf6f2b46901d4f0b38a455851/src/requests/adapters.py#L478-L514","documentation":"InvalidProxyURL is raised in HTTPAdapter.get_connection_with_tls_context when the proxy URL selected for a request parses to no host. requests first prepends 'http://' if the scheme is missing, then runs urllib3's parse_url; if the result still has an empty host (e.g. 'http://' alone, or a value that parses as scheme-only/path-only), it cannot build a proxy connection pool and rejects the URL up front.","triggerScenarios":"proxies={'https': 'http://'} or proxies={'https': ':8080'} or an empty-host value like 'http://:3128'; HTTP_PROXY/HTTPS_PROXY env vars set to malformed values (a bare scheme, a stray colon, unencoded special characters in credentials that break parsing). Raised at src/requests/adapters.py:495-499 during every send when a proxy matches the request URL.","commonSituations":"Env vars exported with empty or placeholder values in CI/shell profiles (export HTTPS_PROXY=http://); templated config where the proxy host variable expanded to empty; passwords containing @ or : that aren't percent-encoded, corrupting the parsed host; copy-paste of 'host:port' handled fine, but 'http://user:p@ss@host' failing.","solutions":["Print/inspect the effective proxy (session.proxies plus HTTP_PROXY/HTTPS_PROXY/ALL_PROXY env vars) and fix or unset the malformed one","Ensure the proxy URL has a host: 'http://proxyhost:3128', not 'http://' or ':3128'","Percent-encode special characters in proxy credentials (urllib.parse.quote for user/password)","If no proxy is wanted, pass proxies={} or use Session(trust_env=False) so stray env vars are ignored"],"exampleFix":"# before\nos.environ['HTTPS_PROXY'] = 'http://'  # placeholder left in CI\nrequests.get(url)  # InvalidProxyURL\n\n# after\nfrom urllib.parse import quote\nproxy = f'http://{quote(user)}:{quote(pw)}@proxy.corp.example:3128'\nrequests.get(url, proxies={'https': proxy, 'http': proxy})","handlingStrategy":"validation","validationCode":"from urllib.parse import urlparse\nparsed = urlparse(proxy_url)\nif not parsed.scheme or not parsed.hostname:\n    raise ValueError(f\"Proxy URL missing scheme or host: {proxy_url!r}\")","typeGuard":null,"tryCatchPattern":"from requests.exceptions import InvalidProxyURL\ntry:\n    resp = session.get(url, proxies=proxies)\nexcept InvalidProxyURL as e:\n    raise RuntimeError(f\"Malformed proxy URL in config: {proxies}\") from e","preventionTips":["Parse every proxy URL with urllib.parse.urlparse and require both scheme and hostname before use","Include the scheme explicitly (http://host:port) — 'host:port' alone parses with no host","Validate HTTP_PROXY/HTTPS_PROXY environment variables at startup, since requests reads them implicitly"],"tags":["network","proxy","url-parsing","configuration","python"],"analyzedSha":"414f0513c33883adf6f2b46901d4f0b38a455851","analyzedAt":"2026-07-31T19:24:57.696Z","schemaVersion":2}