valyala/fasthttp · error

fasthttp: too many redirects detected when doing the request

Error message

fasthttp: too many redirects detected when doing the request

What it means

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.

Source

Thrown at client.go:1226

	req.Header.SetContentTypeBytes(strPostArgsContentType)
	if postArgs != nil {
		if _, err := postArgs.WriteTo(req.BodyWriter()); err != nil {
			return 0, nil, err
		}
	}

	statusCode, body, err = doRequestFollowRedirectsBuffer(req, dst, url, c)

	return statusCode, body, err
}

var (
	// ErrMissingLocation is returned by clients when the Location header is missing on
	// an HTTP response with a redirect status code.
	ErrMissingLocation = errors.New("fasthttp: missing location header for http redirect")
	// ErrTooManyRedirects is returned by clients when the number of redirects followed
	// exceed the max count.
	ErrTooManyRedirects = errors.New("fasthttp: too many redirects detected when doing the request")
	// ErrRedirectBodyStream is returned by clients when a redirect that keeps the request
	// body is received for a request whose body is a stream. The hop that produced the
	// redirect consumed the stream, so the body cannot be sent again.
	ErrRedirectBodyStream = errors.New("fasthttp: cannot follow a body-preserving redirect for a request with a body stream")

	// ErrHostClientRedirectToDifferentScheme is returned when a HostClient follows a redirect to a different protocol.
	ErrHostClientRedirectToDifferentScheme = errors.New("fasthttp: hostclient can't follow redirects to a different protocol," +
		" please use client instead")
)

const defaultMaxRedirectsCount = 16

// Only drain response streams that are known to be small. Reading an unknown
// length stream can block forever (for example, on an event stream), while
// closing a large stream avoids transferring a body the caller discarded.
const maxResponseBodyDrainSize = 8 * 1024

func closeOrDrainResponseBody(resp *Response, maxDrainSize int) error {

View on GitHub (pinned to c96f600972)

Solutions

  1. Fix the server-side redirect loop (inspect the chain with curl -IL)
  2. Increase Client.MaxRedirectsPerRequest if the chain is legitimately long
  3. Ensure cookies/auth headers are preserved across hops
  4. Check proxy rules that bounce between http and https

Example fix

// before
c := &fasthttp.Client{}
// after
c := &fasthttp.Client{MaxRedirectsPerRequest: 32}
_, _, err := c.DoRedirects(req, resp)
Defensive patterns

Strategy: validation

Validate before calling

if client.MaxRedirectsPerRequest <= 0 || client.MaxRedirectsPerRequest > 64 {
    client.MaxRedirectsPerRequest = 16
}
// pre-flight: curl -IL or HEAD the URL to detect loops

Try / catch

if errors.Is(err, fasthttp.ErrTooManyRedirects) {
    // stop retrying the same URL; alert on the redirect chain
}

Prevention

When it happens

Trigger: 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).

Common situations: Redirect loops caused by auth cookies not being forwarded; trailing-slash loops; http<->https ping-pong from a misconfigured reverse proxy.

Related errors


AI-assisted analysis of valyala/fasthttp@c96f600972 (2026-08-31). Data as JSON: /api/errors/6ab5415f6dd5ef65. Report an issue: GitHub.