{"id":"72b8af6e83026e4e","repo":"encode/httpx","slug":"url-too-long","errorCode":null,"errorMessage":"URL too long","messagePattern":"URL too long","errorType":"validation","errorClass":"InvalidURL","httpStatus":null,"severity":"error","filePath":"httpx/_urlparse.py","lineNumber":219,"sourceCode":"        authority = self.authority\n        return \"\".join(\n            [\n                f\"{self.scheme}:\" if self.scheme else \"\",\n                f\"//{authority}\" if authority else \"\",\n                self.path,\n                f\"?{self.query}\" if self.query is not None else \"\",\n                f\"#{self.fragment}\" if self.fragment is not None else \"\",\n            ]\n        )\n\n\ndef urlparse(url: str = \"\", **kwargs: str | None) -> ParseResult:\n    # Initial basic checks on allowable URLs.\n    # ---------------------------------------\n\n    # Hard limit the maximum allowable URL length.\n    if len(url) > MAX_URL_LENGTH:\n        raise InvalidURL(\"URL too long\")\n\n    # If a URL includes any ASCII control characters including \\t, \\r, \\n,\n    # then treat it as invalid.\n    if any(char.isascii() and not char.isprintable() for char in url):\n        char = next(char for char in url if char.isascii() and not char.isprintable())\n        idx = url.find(char)\n        error = (\n            f\"Invalid non-printable ASCII character in URL, {char!r} at position {idx}.\"\n        )\n        raise InvalidURL(error)\n\n    # Some keyword arguments require special handling.\n    # ------------------------------------------------\n\n    # Coerce \"port\" to a string, if it is provided as an integer.\n    if \"port\" in kwargs:\n        port = kwargs[\"port\"]\n        kwargs[\"port\"] = str(port) if isinstance(port, int) else port","sourceCodeStart":201,"sourceCodeEnd":237,"githubUrl":"https://github.com/encode/httpx/blob/b5addb64f0161ff6bfe94c124ef76f6a1fba5254/httpx/_urlparse.py#L201-L237","documentation":"Raised by urlparse when the input URL string exceeds MAX_URL_LENGTH (65536 characters). httpx enforces a hard cap to prevent pathological memory/CPU use during parsing and percent-encoding. It is raised as InvalidURL before any component is examined.","triggerScenarios":"Calling httpx.URL(huge_string), httpx.Client().get(gigantic_url), or building a URL whose query string is assembled from a very large payload (e.g. base64-ing a file into a GET parameter).","commonSituations":"Encoding binary/file data as a query parameter instead of using a POST body; concatenating many filter parameters into a GET URL; passing a data: URL by mistake; URL built from a huge comma-separated ID list.","solutions":["Move large payloads from the URL to the request body (POST/PUT) or use multipart upload.","Shorten the URL by sending IDs/filters in a JSON body or in fewer batched requests.","Compress large query values (gzip+base64) if a GET is genuinely required.","Pre-check length: if len(url) > 65536: switch to POST."],"exampleFix":"// before\nurl = \"https://api.example.com/items?ids=\" + \",\".join(str(i) for i in range(10**6))\nclient.get(url)  # InvalidURL: URL too long\n\n// after\nclient.post(\"https://api.example.com/items\", json={\"ids\": list(range(10**6))})","handlingStrategy":"validation","validationCode":"from httpx._urlparse import MAX_URL_LENGTH\n\ndef safe_url(url: str) -> str:\n    if len(url) > MAX_URL_LENGTH:\n        raise ValueError(f\"URL length {len(url)} exceeds {MAX_URL_LENGTH}; use POST\")\n    return url\n\nclient.get(safe_url(maybe_huge_url))","typeGuard":"def is_url_length_ok(url: str, limit: int = 65536) -> bool:\n    return len(url) <= limit","tryCatchPattern":"from httpx import InvalidURL\n\ntry:\n    client.get(url)\nexcept InvalidURL as e:\n    if \"too long\" in str(e):\n        # switch to a body-based request\n        client.post(endpoint, json=payload)\n    else:\n        raise","preventionTips":["Never encode large data in GET URLs; use POST/PUT bodies.","Cap dynamically-built query strings at a sane length.","Log url lengths in integration tests to catch regressions."],"tags":["url","validation","limits","config"],"analyzedSha":"b5addb64f0161ff6bfe94c124ef76f6a1fba5254","analyzedAt":"2026-08-04T19:32:56.768Z","schemaVersion":2}