{"record":{"id":"6d21774d7c7d91c8","repo":"valyala/fasthttp","slug":"fasthttp-error-when-reading-response-trailer","errorCode":null,"errorMessage":"fasthttp: error when reading response trailer","messagePattern":"fasthttp: error when reading response trailer","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"header.go","lineNumber":471,"sourceCode":"// Trailers allow the sender to include additional headers at the end of chunked messages.\n//\n// The following trailers are forbidden:\n// 1. necessary for message framing (e.g., Transfer-Encoding and Content-Length),\n// 2. routing (e.g., Host),\n// 3. request modifiers (e.g., controls and conditionals in Section 5 of [RFC7231]),\n// 4. authentication (e.g., see [RFC7235] and [RFC6265]),\n// 5. response control data (e.g., see Section 7.1 of [RFC7231]),\n// 6. determining how to process the payload (e.g., Content-Encoding, Content-Type, Content-Range, and Trailer)\n//\n// Return ErrBadTrailer if contain any forbidden trailers.\nfunc (h *header) AddTrailer(trailer string) error {\n\treturn h.AddTrailerBytes(s2b(trailer))\n}\n\nvar (\n\tErrBadTrailer                    = errors.New(\"fasthttp: contain forbidden trailer\")\n\tErrReadingResponseHeaders        = errors.New(\"fasthttp: error when reading response headers\")\n\tErrReadingResponseTrailer        = errors.New(\"fasthttp: error when reading response trailer\")\n\tErrResponseFirstLineMissingSpace = errors.New(\"fasthttp: cannot find whitespace in the first line of response\")\n\tErrUnexpectedStatusCodeChar      = errors.New(\"fasthttp: unexpected char at the end of status code\")\n\tErrMissingRequestMethod          = errors.New(\"fasthttp: cannot find http request method\")\n\tErrUnsupportedRequestMethod      = errors.New(\"fasthttp: unsupported http request method\")\n\tErrExtraWhitespaceInRequestLine  = errors.New(\"fasthttp: extra whitespace in request line\")\n\tErrEmptyRequestURI               = errors.New(\"fasthttp: requesturi cannot be empty\")\n\tErrDuplicateContentLength        = errors.New(\"fasthttp: duplicate content-length header\")\n\tErrUnsupportedTransferEncoding   = errors.New(\"fasthttp: unsupported transfer-encoding\")\n\tErrNonNumericChars               = errors.New(\"fasthttp: non-numeric chars found\")\n\tErrNeedMore                      = errors.New(\"fasthttp: need more data: cannot find trailing lf\")\n\tErrSmallReadBuffer               = errors.New(\"fasthttp: small read buffer. increase readbuffersize\")\n)\n\n// AddTrailerBytes add Trailer header value for chunked response\n// to indicate which headers will be sent after the body.\n//\n// Use Set to set the trailer header later.\n//","sourceCodeStart":453,"sourceCodeEnd":489,"githubUrl":"https://github.com/valyala/fasthttp/blob/c96f600972c6f4a7a30d664257b340ebe9d60124/header.go#L453-L489","documentation":"ErrReadingResponseTrailer indicates fasthttp failed while reading the trailer section that follows a chunked response body — malformed trailer lines or a truncated chunked stream. It is an exported sentinel from header.go.","triggerScenarios":"Reading a chunked HTTP response that declares trailers but sends malformed trailer lines, closes the connection before the terminating CRLF, or violates chunk framing while trailers are expected.","commonSituations":"Unstable upstream servers/proxies dropping connections at end of chunked bodies; custom servers writing trailers with wrong syntax (missing colon/CRLF); network interruptions during large streamed downloads.","solutions":["Fix or update the server/proxy emitting the malformed chunked trailer","Retry the request; if it's transient network truncation, use DoTimeout/DoRetry-style logic","If trailers are unnecessary, have the server omit the Trailer header and send plain chunked responses","Capture the raw bytes to confirm whether the peer or the client parser is at fault before filing a bug"],"exampleFix":null,"handlingStrategy":"retry","validationCode":"// prefer servers that either send valid trailers or omit the Trailer header\nif strings.EqualFold(resp.Header.Peek(\"Transfer-Encoding\") != nil && !trailerOK, true) { /* retry */ }","typeGuard":null,"tryCatchPattern":"err := client.DoTimeout(req, resp, timeout)\nif err != nil && errors.Is(err, fasthttp.ErrReadingResponseTrailer) {\n    // transient truncation: retry with backoff\n}","preventionTips":["Ensure upstream servers emit well-formed chunked trailers (name: value CRLF)","Avoid proxies known to truncate long chunked responses","Prefer non-chunked responses with Content-Length for large downloads","Set timeouts and bounded retries around streaming reads"],"tags":["go","fasthttp","http","trailer","chunked"],"backgroundTag":"malformed-http-response-trailer","analyzedSha":"c96f600972c6f4a7a30d664257b340ebe9d60124","analyzedAt":"2026-08-31T22:48:28.265Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T05:18:18.240Z"}