python network_error ai_generated true

aiohttp.client_exceptions.TooManyRedirects: 0, message='Too many redirects', url=URL('https://example.com/loop')

ID: python/aiohttp-too-many-redirects

Also available as: JSON · Markdown · 中文
80%Fix Rate
86%Confidence
0Evidence
2024-08-22First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
3.8 active — — —
3.9 active — — —
3.10 active — — —
3.11 active — — —
3.12 active — — —

Root Cause

The server returned more than max_redirects (default 10) redirect responses, often due to a redirect loop or cookie/auth misconfiguration.

generic

中文

服务器返回了超过 max_redirects(默认 10)次重定向响应,通常由重定向循环或 cookie/认证配置错误引起。

Workarounds

  1. 90% success
    Inspect the redirect chain with curl:
    
    curl -sIL https://example.com/loop | grep -i '^location'
  2. 85% success
    Handle redirects manually to detect the loop:
    
    async with session.get(url, allow_redirects=False) as r:
        while r.status in (301, 302, 303, 307, 308):
            url = r.headers['Location']
            r = await session.get(url, allow_redirects=False)
  3. 82% success
    Ensure cookies are sent (some redirect loops happen because auth cookie is missing):
    
    session.cookie_jar.update_cookies({'session': token})

Dead Ends

Common approaches that don't work:

  1. 70% fail

    If it's a true loop, it will still hit 100 and fail.

  2. 50% fail

    You now receive 3xx responses directly and must handle Location yourself.

  3. 90% fail

    Unrelated to redirect count.