valyala/fasthttp · error

fasthttp: cannot find whitespace in the first line of respon

Error message

fasthttp: cannot find whitespace in the first line of response

What it means

fasthttp's response header parser scans the first line of an HTTP response for a space separating the protocol (e.g. 'HTTP/1.1') from the status code. When no whitespace is found in that line, the response is malformed and ErrResponseFirstLineMissingSpace is returned. This guards against non-HTTP garbage or truncated/corrupted responses from an upstream server.

Source

Thrown at header.go:472

//
// The following trailers are forbidden:
// 1. necessary for message framing (e.g., Transfer-Encoding and Content-Length),
// 2. routing (e.g., Host),
// 3. request modifiers (e.g., controls and conditionals in Section 5 of [RFC7231]),
// 4. authentication (e.g., see [RFC7235] and [RFC6265]),
// 5. response control data (e.g., see Section 7.1 of [RFC7231]),
// 6. determining how to process the payload (e.g., Content-Encoding, Content-Type, Content-Range, and Trailer)
//
// Return ErrBadTrailer if contain any forbidden trailers.
func (h *header) AddTrailer(trailer string) error {
	return h.AddTrailerBytes(s2b(trailer))
}

var (
	ErrBadTrailer                    = errors.New("fasthttp: contain forbidden trailer")
	ErrReadingResponseHeaders        = errors.New("fasthttp: error when reading response headers")
	ErrReadingResponseTrailer        = errors.New("fasthttp: error when reading response trailer")
	ErrResponseFirstLineMissingSpace = errors.New("fasthttp: cannot find whitespace in the first line of response")
	ErrUnexpectedStatusCodeChar      = errors.New("fasthttp: unexpected char at the end of status code")
	ErrMissingRequestMethod          = errors.New("fasthttp: cannot find http request method")
	ErrUnsupportedRequestMethod      = errors.New("fasthttp: unsupported http request method")
	ErrExtraWhitespaceInRequestLine  = errors.New("fasthttp: extra whitespace in request line")
	ErrEmptyRequestURI               = errors.New("fasthttp: requesturi cannot be empty")
	ErrDuplicateContentLength        = errors.New("fasthttp: duplicate content-length header")
	ErrUnsupportedTransferEncoding   = errors.New("fasthttp: unsupported transfer-encoding")
	ErrNonNumericChars               = errors.New("fasthttp: non-numeric chars found")
	ErrNeedMore                      = errors.New("fasthttp: need more data: cannot find trailing lf")
	ErrSmallReadBuffer               = errors.New("fasthttp: small read buffer. increase readbuffersize")
)

// AddTrailerBytes add Trailer header value for chunked response
// to indicate which headers will be sent after the body.
//
// Use Set to set the trailer header later.
//
// Trailers are only supported with chunked transfer.

View on GitHub (pinned to c96f600972)

Solutions

  1. Verify you are dialing the correct host:port for an HTTP endpoint and that TLS settings (https:// scheme / TLSClientConfig) match the server.
  2. Check what the peer actually sends on connect with curl -v or netcat; if it is not 'HTTP/1.x <code> ...', fix the target service or proxy.
  3. If an upstream proxy emits nonstandard responses, either fix it or use a client that tolerates it (e.g. SetSkipResponseValidation via custom header handling is not available — switch to net/http or fix the upstream).
  4. Discard pooled connections on error by not reusing the client after such errors; create a fresh HostClient or rely on fasthttp's automatic connection close on parse errors.

Example fix

// before
c := &fasthttp.Client{ Addr: "db.example.com:5432" }
_, body, err := c.Get(nil, "http://db.example.com:5432/") // parse error on non-HTTP service
// after
c := &fasthttp.Client{ Addr: "api.example.com:80" }
_, body, err := c.Get(nil, "http://api.example.com:80/") // real HTTP endpoint
Defensive patterns

Strategy: type-guard

Validate before calling

// Before sending, verify the endpoint speaks HTTP:
// curl -sv http://host:port/ | head -1  -> expect 'HTTP/1.x <code>'
// Also ensure scheme matches TLS: https URL for TLS servers.

Type guard

func isResponseParseError(err error) bool {
    return err == fasthttp.ErrResponseFirstLineMissingSpace ||
        err == fasthttp.ErrUnexpectedStatusCodeChar
}

Try / catch

_, body, err := client.Do(req, resp)
if err == fasthttp.ErrResponseFirstLineMissingSpace {
    // drop conn, mark endpoint unhealthy, alert on wrong-port/TLS misconfig
    return fmt.Errorf("non-HTTP response from upstream: %w", err)
}

Prevention

When it happens

Trigger: Calling Response.Read, HostClient/Client.Do, or DoTimeout against a peer whose first response line contains no space (e.g. 'HTTP/1.1' alone, or a plain-text/HTML error page from a proxy, or a binary greeting from a non-HTTP service) instead of the required 'HTTP/1.1 200 OK' format.

Common situations: Pointing the client at the wrong port (a TCP service that is not HTTP, an HTTPS port without TLS, a database or SMTP server), an intermediate proxy returning a bare status line, or a corrupted/truncated keep-alive connection reused from the pool.

Related errors


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