valyala/fasthttp · error

cannot find whitespace in the first line of response %q

Error message

cannot find whitespace in the first line of response %q

What it means

The response status line must contain a space separating the protocol (e.g. HTTP/1.1) from the status code. fasthttp throws this when the first line of a response has no whitespace at all, meaning the bytes are not a valid HTTP status line.

Source

Thrown at header.go:2844

}

func (h *ResponseHeader) parseFirstLine(buf []byte) (int, error) {
	bNext := buf
	var b []byte
	var err error
	for len(b) == 0 {
		if b, bNext, err = nextLine(bNext); err != nil {
			return 0, err
		}
	}

	// parse protocol
	n := bytes.IndexByte(b, ' ')
	if n < 0 {
		if h.secureErrorLogMessage {
			return 0, ErrResponseFirstLineMissingSpace
		}
		return 0, fmt.Errorf("cannot find whitespace in the first line of response %q", buf)
	}
	protoStr := b[:n]
	b = b[n+1:]
	for len(b) > 0 && b[0] == ' ' {
		b = b[1:]
	}

	// parse status code
	statusCode := b
	statusMessage := []byte(nil)
	if n = bytes.IndexByte(b, ' '); n >= 0 {
		statusCode = b[:n]
		statusMessage = b[n+1:]
	}
	if len(statusCode) != 3 {
		if h.secureErrorLogMessage {
			return 0, ErrUnexpectedStatusCodeChar
		}

View on GitHub (pinned to c96f600972)

Solutions

  1. Verify the target URL scheme — use https:// (and TLS client) if the server requires TLS
  2. Confirm the remote port actually speaks HTTP (nmap/curl to check)
  3. Inspect the raw response in the error (%q of buf) to see what was actually returned
  4. If a proxy is in the path, check it isn't returning a non-HTTP error page

Example fix

// before
c := &fasthttp.Client{} // used against https://host
// after
c := &fasthttp.Client{TLSConfig: &tls.Config{ServerName: "host"}} // with https URL
Defensive patterns

Strategy: validation

Validate before calling

u, _ := url.Parse(target)
if u.Scheme == "https" {
    client.TLSConfig = &tls.Config{ServerName: u.Hostname()}
}

Type guard

func isMalformedFirstLine(err error) bool {
    return err != nil && strings.Contains(err.Error(), "cannot find whitespace in the first line")
}

Try / catch

err := client.Do(req, resp)
if err != nil && strings.Contains(err.Error(), "cannot find whitespace in the first line") {
    return fmt.Errorf("peer did not return HTTP: %w", err)
}

Prevention

When it happens

Trigger: ResponseHeader.Parse (parseFirstLine) reads a first line with no ' ' byte — e.g. the server sent an error page, TLS handshake data, or empty/garbage response.

Common situations: Pointing a fasthttp client at an HTTPS endpoint without TLS (getting plaintext TLS alerts or HTML); hitting a non-HTTP service on the port; captive portals or proxies returning HTML error pages; receiving an HTTP/0.9-style bare response.

Related errors


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