{"id":"fca5befc335cc48f","repo":"encode/httpx","slug":"url-component-key-too-long","errorCode":null,"errorMessage":"URL component '{key}' too long","messagePattern":"URL component '(.+?)' too long","errorType":"validation","errorClass":"InvalidURL","httpStatus":null,"severity":"error","filePath":"httpx/_urlparse.py","lineNumber":269,"sourceCode":"    if \"raw_path\" in kwargs:\n        raw_path = kwargs.pop(\"raw_path\") or \"\"\n        kwargs[\"path\"], seperator, kwargs[\"query\"] = raw_path.partition(\"?\")\n        if not seperator:\n            kwargs[\"query\"] = None\n\n    # Ensure that IPv6 \"host\" addresses are always escaped with \"[...]\".\n    if \"host\" in kwargs:\n        host = kwargs.get(\"host\") or \"\"\n        if \":\" in host and not (host.startswith(\"[\") and host.endswith(\"]\")):\n            kwargs[\"host\"] = f\"[{host}]\"\n\n    # If any keyword arguments are provided, ensure they are valid.\n    # -------------------------------------------------------------\n\n    for key, value in kwargs.items():\n        if value is not None:\n            if len(value) > MAX_URL_LENGTH:\n                raise InvalidURL(f\"URL component '{key}' too long\")\n\n            # If a component 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 value):\n                char = next(\n                    char for char in value if char.isascii() and not char.isprintable()\n                )\n                idx = value.find(char)\n                error = (\n                    f\"Invalid non-printable ASCII character in URL {key} component, \"\n                    f\"{char!r} at position {idx}.\"\n                )\n                raise InvalidURL(error)\n\n            # Ensure that keyword arguments match as a valid regex.\n            if not COMPONENT_REGEX[key].fullmatch(value):\n                raise InvalidURL(f\"Invalid URL component '{key}'\")\n","sourceCodeStart":251,"sourceCodeEnd":287,"githubUrl":"https://github.com/encode/httpx/blob/b5addb64f0161ff6bfe94c124ef76f6a1fba5254/httpx/_urlparse.py#L251-L287","documentation":"Per-component length cap: each individual URL component passed as a kwarg to httpx.URL/urlparse (scheme, host, path, query, etc.) must not exceed MAX_URL_LENGTH (65536 chars). Distinct from the whole-URL check; applied during the kwargs validation loop.","triggerScenarios":"httpx.URL(path='/' + 'a'*100000), httpx.URL(query='x=' + 'y'*200000), or building URLs by passing a giant component via kwargs rather than the url string.","commonSituations":"Passing a huge base64 blob as the 'query' kwarg; building a path component from an unbounded list; migrating a long query string into the kwargs API.","solutions":["Move oversized data to the request body (POST/PUT json=... or data=...).","Shorten or paginate the offending component before constructing the URL.","Validate component length before construction: assert len(value) <= 65536.","Compress large query values when GET semantics are mandatory."],"exampleFix":"// before\nurl = httpx.URL(\"https://api.example.com/search\", query=\"q=\" + \"a\"*100000)  # InvalidURL\n\n// after\nclient.post(\"https://api.example.com/search\", json={\"q\": \"a\" * 100000})","handlingStrategy":"validation","validationCode":"from httpx._urlparse import MAX_URL_LENGTH\n\ndef safe_component(key: str, value: str) -> str:\n    if len(value) > MAX_URL_LENGTH:\n        raise ValueError(f\"Component {key!r} too long ({len(value)} chars)\")\n    return value\n\nurl = httpx.URL(\"https://example.com\", query=safe_component(\"query\", q))","typeGuard":"def component_ok(value: str, limit: int = 65536) -> bool:\n    return len(value) <= limit","tryCatchPattern":"from httpx import InvalidURL\n\ntry:\n    url = httpx.URL(base, query=huge_query)\nexcept InvalidURL as e:\n    if \"too long\" in str(e):\n        client.post(base, json={\"q\": huge_query})\n    else:\n        raise","preventionTips":["Bound component sizes at the source (paginate, compress).","Use the body for large payloads instead of kwargs.","Assert length invariants in unit tests for URL builders."],"tags":["url","validation","limits"],"analyzedSha":"b5addb64f0161ff6bfe94c124ef76f6a1fba5254","analyzedAt":"2026-08-04T19:32:56.768Z","schemaVersion":2}