{"record":{"id":"7e4bcf61c9222b7d","repo":"xtekky/gpt4free","slug":"bad-url-url","errorCode":null,"errorMessage":"Bad url: {url}","messagePattern":"Bad url: (.+?)","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"g4f/requests/__init__.py","lineNumber":112,"sourceCode":"\n\ndef get_cookie_params_from_dict(\n    cookies: Cookies, url: str = None, domain: str = None\n) -> list[CookieParam]:\n    return [\n        CookieParam.from_json(\n            {\"name\": key, \"value\": value, \"url\": url, \"domain\": domain}\n        )\n        for key, value in cookies.items()\n    ]\n\n\nasync def clear_cookies_for_url(\n    browser: Browser, url: str, ignore_cookies: list[str] = None\n):\n    host = urlparse(url).hostname\n    if not host:\n        raise ValueError(f\"Bad url: {url}\")\n\n    if ignore_cookies is None:\n        ignore_cookies = []\n    tab = browser.main_tab  # any open tab is fine\n    cookies = (\n        await browser.cookies.get_all()\n    )  # returns CDP cookies :contentReference[oaicite:2]{index=2}\n    for c in cookies:\n        dom = (c.domain or \"\").lstrip(\".\")\n        if dom and (host == dom or host.endswith(\".\" + dom)):\n            if c.name in ignore_cookies:\n                continue\n            await tab.send(\n                nodriver.cdp.network.delete_cookies(\n                    name=c.name,\n                    domain=dom,  # exact domain :contentReference[oaicite:3]{index=3}\n                    path=c.path,  # exact path :contentReference[oaicite:4]{index=4}\n                    # partition_key=c.partition_key,  # if you use partitioned cookies","sourceCodeStart":94,"sourceCodeEnd":130,"githubUrl":"https://github.com/xtekky/gpt4free/blob/973504e1770928ed5fb82f43da528f441ad9ddc3/g4f/requests/__init__.py#L94-L130","documentation":"Thrown by clear_cookies_for_url() in g4f/requests/__init__.py when urlparse(url).hostname returns None, meaning the passed string has no parsable host component. The function needs a hostname to match cookies against CDP cookie domains, so a host-less URL cannot be processed. It is a caller-input validation error, not a network or browser error.","triggerScenarios":"Calling await clear_cookies_for_url(browser, url) with an empty string, a relative path like \"/path\", a scheme-only string like \"http://\", or a non-URL value such as \"about:blank\" or \"data:text/html,...\". Any input where urlparse cannot extract a hostname triggers it before any browser interaction happens.","commonSituations":"Passing a provider base URL read from config that is empty or misconfigured; forwarding a response redirect URL or window.location value that turned out to be a blank/special page; copy-paste typos like \"htp://example.com\" that yield no hostname.","solutions":["Inspect the url value right before the call and fix the upstream string so it is a full absolute URL including scheme and host (e.g. https://example.com).","If the URL comes from configuration, correct the config entry / environment variable to include the scheme.","Guard the call site with urlparse(url).hostname and skip/log when it is None.","Catch ValueError locally only as a last resort, logging the offending url for diagnosis."],"exampleFix":"// before\nawait clear_cookies_for_url(browser, target_url)  # target_url = \"\"\n\n// after\nfrom urllib.parse import urlparse\nif urlparse(target_url).hostname:\n    await clear_cookies_for_url(browser, target_url)\nelse:\n    debug.log(f\"Skipping cookie clear, no host in url: {target_url}\")","handlingStrategy":"validation","validationCode":"from urllib.parse import urlparse\n\ndef has_host(url: str) -> bool:\n    return bool(urlparse(url).hostname)","typeGuard":"def is_clearable_url(url: str) -> bool:\n    \"\"\"True when clear_cookies_for_url can process this url.\"\"\"\n    return isinstance(url, str) and urlparse(url).hostname is not None","tryCatchPattern":"try:\n    await clear_cookies_for_url(browser, url)\nexcept ValueError as e:\n    debug.log(f\"Skipped cookie clearing: {e}\")  # bad url is not fatal","preventionTips":["Always build URLs from a verified base (scheme + host) before passing to cookie helpers.","Validate config-supplied URLs once at startup with urlparse().hostname.","Treat blank/special pages (about:blank, data:) as non-clearable and filter them early."],"tags":["url-validation","cookies","valueerror","input-validation"],"backgroundTag":null,"analyzedSha":"973504e1770928ed5fb82f43da528f441ad9ddc3","analyzedAt":"2026-08-14T23:45:32.408Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}