{"id":"6cf422060ac3f4c0","repo":"aio-libs/aiohttp","slug":"server-attempted-redirecting-to-a-location-that-do","errorCode":null,"errorMessage":"Server attempted redirecting to a location that does not look like a URL","messagePattern":"Server attempted redirecting to a location that does not look like a URL","errorType":"exception","errorClass":"InvalidUrlRedirectClientError","httpStatus":null,"severity":"error","filePath":"aiohttp/client.py","lineNumber":816,"sourceCode":"                            hdrs.URI\n                        )\n                        if r_url is None:\n                            # see github.com/aio-libs/aiohttp/issues/2022\n                            break\n                        else:\n                            # reading from correct redirection\n                            # response is forbidden\n                            resp.release()\n\n                        try:\n                            parsed_redirect_url = URL(\n                                r_url, encoded=not self._requote_redirect_url\n                            )\n                        except ValueError as e:\n                            if req._body is not None:\n                                await req._body.close()\n                            resp.close()\n                            raise InvalidUrlRedirectClientError(\n                                r_url,\n                                \"Server attempted redirecting to a location that does not look like a URL\",\n                            ) from e\n\n                        scheme = parsed_redirect_url.scheme\n                        if scheme not in HTTP_AND_EMPTY_SCHEMA_SET:\n                            if req._body is not None:\n                                await req._body.close()\n                            resp.close()\n                            raise NonHttpUrlRedirectClientError(r_url)\n                        elif not scheme:\n                            parsed_redirect_url = url.join(parsed_redirect_url)\n\n                        try:\n                            redirect_origin = parsed_redirect_url.origin()\n                        except ValueError as origin_val_err:\n                            if req._body is not None:\n                                await req._body.close()","sourceCodeStart":798,"sourceCodeEnd":834,"githubUrl":"https://github.com/aio-libs/aiohttp/blob/c0ef574e29109210e96e652771ae4e7b88615fa4/aiohttp/client.py#L798-L834","documentation":"Raised as `InvalidUrlRedirectClientError` (client.py:808-819) when the `Location` (or `URI`) header from a 3xx response cannot be parsed by yarl's `URL(...)` constructor. The exception chains from the underlying `ValueError`. This is distinct from a non-http scheme redirect (which raises `NonHttpUrlRedirectClientError`) — this one means the value isn't even URL-shaped.","triggerScenarios":"Server returns `Location: <<<garbage>>>`, a `Location` with illegal characters, an empty/malformed URI template, or binary content in the header. The `encoded=` flag depends on `requote_redirect_url`.","commonSituations":"Misconfigured server returning a templated Location without substitution; upstream proxy rewriting the header; clients hitting a dev/staging server with broken redirect logic; reverse proxies that prepend a bad prefix.","solutions":["Set `allow_redirects=False` and handle the Location header yourself (validate/normalize before re-requesting).","Fix the server's redirect target — aiohttp is correctly refusing a malformed URL.","If the server returns an unquoted URL, try `ClientSession(requote_redirect_url=False)` to skip requoting (note: still must be parseable)."],"exampleFix":"// before\nresp = await session.get(url)  # server Location is malformed -> raise\n// after\nresp = await session.get(url, allow_redirects=False)\nloc = resp.headers.get('Location')\nif loc and valid_url(loc):\n    resp = await session.get(session._build_url(loc))","handlingStrategy":"validation","validationCode":"from yarl import URL\n\ndef valid_redirect_target(loc: str) -> bool:\n    try:\n        URL(loc)\n        return True\n    except ValueError:\n        return False","typeGuard":null,"tryCatchPattern":"from aiohttp import InvalidUrlRedirectClientError\n\ntry:\n    resp = await session.get(url)\nexcept InvalidUrlRedirectClientError as e:\n    # fall back to manual handling with allow_redirects=False\n    resp = await session.get(url, allow_redirects=False)","preventionTips":["Use allow_redirects=False against servers you don't control.","Validate the Location header yourself before following.","Log upstream redirect targets to catch server misconfigurations early."],"tags":["client","redirect","url","server-misbehavior"],"analyzedSha":"c0ef574e29109210e96e652771ae4e7b88615fa4","analyzedAt":"2026-08-04T19:51:05.467Z","schemaVersion":2}