valyala/fasthttp · error

fasthttp: cannot find http request method

Error message

fasthttp: cannot find http request method

What it means

fasthttp requires every HTTP/1.x request to carry a method token before the request-URI. When parsing or validating a Request whose method is empty (RequestHeader.Method is empty string and no default was applied), ErrMissingRequestMethod is returned. It signals an incomplete request line that cannot be serialized/parsed.

Source

Thrown at header.go:474

// 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.
// Trailers allow the sender to include additional headers at the end of chunked messages.
//

View on GitHub (pinned to c96f600972)

Solutions

  1. Call req.Header.SetMethod(fasthttp.MethodGet) (or the appropriate method) before sending the request.
  2. For reused/reset Request objects, re-set Method after Reset since Reset clears it.
  3. If building headers by hand, append the method token first: 'GET /path HTTP/1.1\r\n'.
  4. On the server side, return 400 to clients that omit the method instead of treating it as an application bug.

Example fix

// before
var req fasthttp.Request
req.SetRequestURI("/api/v1") // no method set
// after
var req fasthttp.Request
req.Header.SetMethod(fasthttp.MethodPost)
req.SetRequestURI("/api/v1")
Defensive patterns

Strategy: validation

Validate before calling

if req.Header.Method == "" {
    req.Header.SetMethod(fasthttp.MethodGet) // or reject
}
if !knownMethods[req.Header.Method] {
    return errors.New("method missing or invalid")
}

Type guard

func hasValidMethod(h *fasthttp.RequestHeader) bool {
    m := string(h.Method())
    switch m {
    case "GET", "POST", "PUT", "DELETE", "HEAD", "OPTIONS", "PATCH", "CONNECT", "TRACE":
        return true
    }
    return false
}

Try / catch

if err := client.Do(req, resp); err == fasthttp.ErrMissingRequestMethod {
    return fmt.Errorf("request built without method: %w", err)
}

Prevention

When it happens

Trigger: Manually building a RequestHeader/Request without setting Method (h.SetMethod or req.Header.Method) and then writing/reading it; constructing a raw request buffer via header.AppendBytes or Write where the method field was never populated; parsing a malformed incoming request with no method token.

Common situations: Low-level RequestHeader manipulation (e.g. proxy code that rebuilds headers), forgetting to call req.Header.SetMethod on a reused zero-value Request, or a hostile client sending a request line missing the method to your fasthttp server.

Related errors


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