valyala/fasthttp · error

fasthttp: requesturi cannot be empty

Error message

fasthttp: requesturi cannot be empty

What it means

An HTTP request line must contain a request-target (URI/path) after the method. fasthttp returns ErrEmptyRequestURI when the request URI is missing or empty while building or parsing a request, because a request without a target cannot be routed or sent.

Source

Thrown at header.go:477

// 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.
//
// The following trailers are forbidden:
// 1. necessary for message framing (e.g., Transfer-Encoding and Content-Length),
// 2. routing (e.g., Host),

View on GitHub (pinned to c96f600972)

Solutions

  1. Always call req.SetRequestURI("http://host/path") (or SetRequestURIBytes) before Do/Write.
  2. Check conditional mutation logic — ensure the branch that clears or skips SetRequestURI cannot run.
  3. For reused pooled Request objects, re-set URI after Reset() since Reset wipes it.
  4. On server side, respond 400 to clients that omit the request target.

Example fix

// before
req := fasthttp.AcquireRequest()
req.Header.SetMethod(fasthttp.MethodGet)
client.Do(req, resp) // empty requestURI
// after
req := fasthttp.AcquireRequest()
req.Header.SetMethod(fasthttp.MethodGet)
req.SetRequestURI("https://example.com/api")
client.Do(req, resp)
Defensive patterns

Strategy: validation

Validate before calling

if len(req.Header.RequestURI()) == 0 {
    return errors.New("requestURI is empty; call SetRequestURI before Do")
}
// optionally validate scheme+host too:
// u := fasthttp.AcquireURI(); defer fasthttp.ReleaseURI(u)
// req.URI().CopyTo(u)

Type guard

func hasRequestURI(req *fasthttp.Request) bool {
    return len(req.Header.RequestURI()) > 0
}

Try / catch

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

Prevention

When it happens

Trigger: Calling Request.Write/Client.Do with a Request whose RequestURI was never set (req.SetRequestURI not called), resetting a reused Request and forgetting to re-set the URI, or parsing a request line like 'GET HTTP/1.1' with no target.

Common situations: Proxy/middleware code that mutates req.SetRequestURIBytes conditionally so the URI is dropped on some paths; HostClient.Do with req URI set only via Host header; zero-value Request structs passed to Do.

Related errors


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