{"record":{"id":"b26b88489ce03bfe","repo":"valyala/fasthttp","slug":"fasthttp-error-when-reading-response-headers","errorCode":null,"errorMessage":"fasthttp: error when reading response headers","messagePattern":"fasthttp: error when reading response headers","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"header.go","lineNumber":470,"sourceCode":"// Trailers are only supported with chunked transfer.\n// 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.","sourceCodeStart":452,"sourceCodeEnd":488,"githubUrl":"https://github.com/valyala/fasthttp/blob/c96f600972c6f4a7a30d664257b340ebe9d60124/header.go#L452-L488","documentation":"ErrReadingResponseHeaders indicates fasthttp failed while parsing an HTTP response's header section from the wire — malformed start line, invalid header syntax, or truncated input. It is an exported sentinel from header.go used (possibly wrapped) when header parsing errors occur.","triggerScenarios":"Client calls (Do/DoTimeout/DoRedirect) receiving a response whose header bytes are malformed: missing space in status line, non-ASCII/garbage bytes, connection closed mid-headers, or a non-HTTP proxy response.","commonSituations":"Target server is actually speaking TLS to a plain-HTTP client (or vice versa); intermediate proxies/CDNs returning HTML error pages; UDP/TCP misrouting; server sending HTTP/2 to an HTTP/1.1-only client.","solutions":["Verify the endpoint scheme/port (https:// vs http://) matches the server","Inspect the raw response (curl -v) to see what the server actually sends","Check for intercepting proxies/VPN/CDN mangling responses and set proper Proxy settings","Increase ReadTimeout/retry, or upgrade fasthttp if parsing a valid but unusual header fails"],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":"// verify endpoint scheme/port before requesting\nu, _ := url.Parse(target)\nif u.Scheme == \"https\" && !clientTLS { return errors.New(\"client not configured for TLS\") }","typeGuard":null,"tryCatchPattern":"err := client.Do(req, resp)\nif err != nil {\n    if errors.Is(err, fasthttp.ErrReadingResponseHeaders) {\n        // log raw bytes, check scheme/proxy, retry once\n    }\n}","preventionTips":["Match URL scheme to server protocol (https vs http vs h2c)","Bypass or correctly configure proxies/CDNs that inject HTML errors","Set sane ReadTimeout and retry transient failures","curl -v the endpoint to see the exact bytes the server returns"],"tags":["go","fasthttp","http","response-parsing"],"backgroundTag":"malformed-http-response-headers","analyzedSha":"c96f600972c6f4a7a30d664257b340ebe9d60124","analyzedAt":"2026-08-31T22:48:28.265Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T05:18:18.240Z"}