valyala/fasthttp · error

invalid response status code %q: response %q

Error message

invalid response status code %q: response %q

What it means

The status code in the response's first line must be exactly 3 digits. fasthttp throws this when the extracted status-code token has a length other than 3, indicating a malformed or non-HTTP response line.

Source

Thrown at header.go:2863

	}
	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
		}
		return 0, fmt.Errorf("invalid response status code %q: response %q", statusCode, buf)
	}
	h.statusCode, n, err = parseUintBuf(statusCode)
	if err != nil || n != 3 {
		if h.secureErrorLogMessage {
			return 0, ErrUnexpectedStatusCodeChar
		}
		return 0, fmt.Errorf("invalid response status code %q: response %q", statusCode, buf)
	}
	if !isHTTPVersion(protoStr) {
		if h.secureErrorLogMessage {
			return 0, fmt.Errorf("unsupported http version %q", protoStr)
		}
		return 0, fmt.Errorf("unsupported http version %q in %q", protoStr, buf)
	}
	h.noHTTP11 = !bytes.Equal(protoStr, strHTTP11)
	h.protocol = append(h.protocol[:0], protoStr...)
	if len(statusMessage) > 0 {
		h.SetStatusMessage(statusMessage)

View on GitHub (pinned to c96f600972)

Solutions

  1. Check what the endpoint actually returns (curl -v) — the peer is not sending valid HTTP
  2. Fix the URL/scheme/port if you're hitting the wrong service
  3. If a proxy rewrites status lines, bypass or fix the proxy
  4. Note: with SecureErrorLogMessage set, this surfaces as ErrUnexpectedStatusCodeChar without echoing the response
Defensive patterns

Strategy: validation

Type guard

func isBadStatusCodeToken(err error) bool {
    return err != nil && strings.Contains(err.Error(), "invalid response status code")
}

Try / catch

err := client.Do(req, resp)
if err != nil && strings.Contains(err.Error(), "invalid response status code") {
    log.Debug("non-HTTP peer response", "err", err)
    return errUpstreamNotHTTP
}

Prevention

When it happens

Trigger: parseFirstLine splits the status line and the token between the first and second spaces is not 3 characters long — e.g. 'HTTP/1.1 200' with no message, or garbage tokens.

Common situations: Non-HTTP servers replying on the port; HTTP/0.9-style responses; proxies returning custom error strings; truncated responses from broken intermediaries.

Related errors


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