{"record":{"id":"6ab5415f6dd5ef65","repo":"valyala/fasthttp","slug":"fasthttp-too-many-redirects-detected-when-doing-t","errorCode":null,"errorMessage":"fasthttp: too many redirects detected when doing the request","messagePattern":"fasthttp: too many redirects detected when doing the request","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"client.go","lineNumber":1226,"sourceCode":"\treq.Header.SetContentTypeBytes(strPostArgsContentType)\n\tif postArgs != nil {\n\t\tif _, err := postArgs.WriteTo(req.BodyWriter()); err != nil {\n\t\t\treturn 0, nil, err\n\t\t}\n\t}\n\n\tstatusCode, body, err = doRequestFollowRedirectsBuffer(req, dst, url, c)\n\n\treturn statusCode, body, err\n}\n\nvar (\n\t// ErrMissingLocation is returned by clients when the Location header is missing on\n\t// an HTTP response with a redirect status code.\n\tErrMissingLocation = errors.New(\"fasthttp: missing location header for http redirect\")\n\t// ErrTooManyRedirects is returned by clients when the number of redirects followed\n\t// exceed the max count.\n\tErrTooManyRedirects = errors.New(\"fasthttp: too many redirects detected when doing the request\")\n\t// ErrRedirectBodyStream is returned by clients when a redirect that keeps the request\n\t// body is received for a request whose body is a stream. The hop that produced the\n\t// redirect consumed the stream, so the body cannot be sent again.\n\tErrRedirectBodyStream = errors.New(\"fasthttp: cannot follow a body-preserving redirect for a request with a body stream\")\n\n\t// ErrHostClientRedirectToDifferentScheme is returned when a HostClient follows a redirect to a different protocol.\n\tErrHostClientRedirectToDifferentScheme = errors.New(\"fasthttp: hostclient can't follow redirects to a different protocol,\" +\n\t\t\" please use client instead\")\n)\n\nconst defaultMaxRedirectsCount = 16\n\n// Only drain response streams that are known to be small. Reading an unknown\n// length stream can block forever (for example, on an event stream), while\n// closing a large stream avoids transferring a body the caller discarded.\nconst maxResponseBodyDrainSize = 8 * 1024\n\nfunc closeOrDrainResponseBody(resp *Response, maxDrainSize int) error {","sourceCodeStart":1208,"sourceCodeEnd":1244,"githubUrl":"https://github.com/valyala/fasthttp/blob/c96f600972c6f4a7a30d664257b340ebe9d60124/client.go#L1208-L1244","documentation":"ErrTooManyRedirects is returned when the client follows more redirects than the configured maximum (default maxRedirectsCount = 16) for a single request. fasthttp stops to protect against redirect loops.","triggerScenarios":"Calling a redirect-following client (e.g. Client.DoRedirects) against a URL whose redirect chain exceeds Client.MaxRedirectsPerRequest, most often an infinite redirect loop (A -> B -> A).","commonSituations":"Redirect loops caused by auth cookies not being forwarded; trailing-slash loops; http<->https ping-pong from a misconfigured reverse proxy.","solutions":["Fix the server-side redirect loop (inspect the chain with curl -IL)","Increase Client.MaxRedirectsPerRequest if the chain is legitimately long","Ensure cookies/auth headers are preserved across hops","Check proxy rules that bounce between http and https"],"exampleFix":"// before\nc := &fasthttp.Client{}\n// after\nc := &fasthttp.Client{MaxRedirectsPerRequest: 32}\n_, _, err := c.DoRedirects(req, resp)","handlingStrategy":"validation","validationCode":"if client.MaxRedirectsPerRequest <= 0 || client.MaxRedirectsPerRequest > 64 {\n    client.MaxRedirectsPerRequest = 16\n}\n// pre-flight: curl -IL or HEAD the URL to detect loops","typeGuard":null,"tryCatchPattern":"if errors.Is(err, fasthttp.ErrTooManyRedirects) {\n    // stop retrying the same URL; alert on the redirect chain\n}","preventionTips":["Check redirect chains with curl -IL before wiring clients","Ensure cookies/auth survive across hops","Fix http<->https redirect loops at the proxy","Set a sane MaxRedirectsPerRequest instead of relying on defaults"],"tags":["http","redirect","client","loop"],"backgroundTag":"too-many-redirects","analyzedSha":"c96f600972c6f4a7a30d664257b340ebe9d60124","analyzedAt":"2026-08-31T22:48:28.265Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T05:18:18.240Z"}