{"record":{"id":"135a5c1a83a452a0","repo":"valyala/fasthttp","slug":"fasthttp-body-size-exceeds-the-given-limit","errorCode":null,"errorMessage":"fasthttp: body size exceeds the given limit","messagePattern":"fasthttp: body size exceeds the given limit","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"http.go","lineNumber":2807,"sourceCode":"\t}\n\tif _, err := w.Write(strCRLF); err != nil {\n\t\treturn err\n\t}\n\tif _, err := w.Write(b); err != nil {\n\t\treturn err\n\t}\n\t// If is end chunk, write CRLF after writing trailer\n\tif n > 0 {\n\t\tif _, err := w.Write(strCRLF); err != nil {\n\t\t\treturn err\n\t\t}\n\t}\n\treturn w.Flush()\n}\n\n// ErrBodyTooLarge is returned if either request or response body exceeds\n// the given limit.\nvar ErrBodyTooLarge = errors.New(\"fasthttp: body size exceeds the given limit\")\n\nfunc copyZeroAllocWithLimit(w io.Writer, r io.Reader, maxBodySize int) (int64, error) {\n\tif maxBodySize <= 0 {\n\t\treturn copyZeroAlloc(w, r)\n\t}\n\n\tlr := &io.LimitedReader{\n\t\tR: r,\n\t\tN: int64(maxBodySize) + 1,\n\t}\n\tn, err := copyZeroAlloc(w, lr)\n\tif err != nil {\n\t\treturn n, err\n\t}\n\tif lr.N <= 0 {\n\t\treturn n, ErrBodyTooLarge\n\t}\n\treturn n, nil","sourceCodeStart":2789,"sourceCodeEnd":2825,"githubUrl":"https://github.com/valyala/fasthttp/blob/c96f600972c6f4a7a30d664257b340ebe9d60124/http.go#L2789-L2825","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Raise the limit: set Server.MaxResponseBodySize or Client.ReadBufferSize/MaxResponseBodySize (client: c.MaxResponseBodySize or via Client struct) to a value covering your payloads.","For large payloads, stream instead of buffering: use resp.BodyStream() or BodyWriteTo to consume without the whole-body limit applying the same way.","If serving, check the client's declared Content-Length or streamed size against your configured limit and reject early with 413."],"exampleFix":"// before\nc := &fasthttp.Client{}\nresp, err := c.Get(nil, \"https://example.com/bigfile\") // ErrBodyTooLarge\n// after\nc := &fasthttp.Client{MaxResponseBodySize: 100 * 1024 * 1024}\nresp, err := c.Get(nil, \"https://example.com/bigfile\")","handlingStrategy":"try-catch","validationCode":"cl := resp.Header.ContentLength()\nif cl > maxAllowed {\n    return fmt.Errorf(\"response too large: %d > %d\", cl, maxAllowed)\n}","typeGuard":null,"tryCatchPattern":"err := client.Do(req, resp)\nif errors.Is(err, fasthttp.ErrBodyTooLarge) {\n    // stream instead or raise limit\n    resp := fasthttp.AcquireResponse()\n    stream := resp.BodyStream()\n    _, _ = io.Copy(sink, stream)\n}","preventionTips":["Set MaxResponseBodySize explicitly for every client/server.","Use BodyStream for potentially large payloads.","Alert on 413s and body-limit errors to right-size limits."],"tags":["http","body-limit","payload-size"],"backgroundTag":"body-size-limit-exceeded","analyzedSha":"c96f600972c6f4a7a30d664257b340ebe9d60124","analyzedAt":"2026-08-31T22:48:28.265Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T05:18:18.240Z"}