valyala/fasthttp · error

fasthttp: body size exceeds the given limit

Error message

fasthttp: body size exceeds the given limit

What it means

fasthttp returns ErrBodyTooLarge when reading a request or response body whose size exceeds the configured limit (maxResponseBodySize / MaxResponseBodySize, or the limit passed to streaming copy helpers like copyZeroAllocWithLimit). It protects the server and client from unbounded memory use.

Source

Thrown at http.go:2807

	}
	if _, err := w.Write(strCRLF); err != nil {
		return err
	}
	if _, err := w.Write(b); err != nil {
		return err
	}
	// If is end chunk, write CRLF after writing trailer
	if n > 0 {
		if _, err := w.Write(strCRLF); err != nil {
			return err
		}
	}
	return w.Flush()
}

// ErrBodyTooLarge is returned if either request or response body exceeds
// the given limit.
var ErrBodyTooLarge = errors.New("fasthttp: body size exceeds the given limit")

func copyZeroAllocWithLimit(w io.Writer, r io.Reader, maxBodySize int) (int64, error) {
	if maxBodySize <= 0 {
		return copyZeroAlloc(w, r)
	}

	lr := &io.LimitedReader{
		R: r,
		N: int64(maxBodySize) + 1,
	}
	n, err := copyZeroAlloc(w, lr)
	if err != nil {
		return n, err
	}
	if lr.N <= 0 {
		return n, ErrBodyTooLarge
	}
	return n, nil

View on GitHub (pinned to c96f600972)

Solutions

  1. Raise the limit: set Server.MaxResponseBodySize or Client.ReadBufferSize/MaxResponseBodySize (client: c.MaxResponseBodySize or via Client struct) to a value covering your payloads.
  2. For large payloads, stream instead of buffering: use resp.BodyStream() or BodyWriteTo to consume without the whole-body limit applying the same way.
  3. If serving, check the client's declared Content-Length or streamed size against your configured limit and reject early with 413.

Example fix

// before
c := &fasthttp.Client{}
resp, err := c.Get(nil, "https://example.com/bigfile") // ErrBodyTooLarge
// after
c := &fasthttp.Client{MaxResponseBodySize: 100 * 1024 * 1024}
resp, err := c.Get(nil, "https://example.com/bigfile")
Defensive patterns

Strategy: try-catch

Validate before calling

cl := resp.Header.ContentLength()
if cl > maxAllowed {
    return fmt.Errorf("response too large: %d > %d", cl, maxAllowed)
}

Try / catch

err := client.Do(req, resp)
if errors.Is(err, fasthttp.ErrBodyTooLarge) {
    // stream instead or raise limit
    resp := fasthttp.AcquireResponse()
    stream := resp.BodyStream()
    _, _ = io.Copy(sink, stream)
}

Prevention

When it happens

Trigger: Receiving a response or request whose Content-Length or chunked body exceeds the configured max body size; calling bodyBytesStream / copy helpers with maxBodySize set and the stream exceeding it; server handler reading a body larger than Server.MaxResponseBodySize.

Common situations: Downloading a large file with default client limits; uploading an oversized payload to a server with default 4MB limit; a proxied endpoint returning big responses; limits tightened in config without adjusting consumers.

Related errors


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