gorilla/mux · error
mux: route doesn't have methods
Error message
mux: route doesn't have methods
What it means
Returned by Route.GetMethods (route.go:767-776) when iterating the route's matchers and none is a methodMatcher, i.e. .Methods(...) was never called on the route (it accepts every method).
Source
Thrown at route.go:776
queries = append(queries, query.template)
}
return queries, nil
}
// GetMethods returns the methods the route matches against
// This is useful for building simple REST API documentation and for instrumentation
// against third-party services.
// An error will be returned if route does not have methods.
func (r *Route) GetMethods() ([]string, error) {
if r.err != nil {
return nil, r.err
}
for _, m := range r.matchers {
if methods, ok := m.(methodMatcher); ok {
return []string(methods), nil
}
}
return nil, errors.New("mux: route doesn't have methods")
}
// GetHostTemplate returns the template used to build the
// route match.
// This is useful for building simple REST API documentation and for instrumentation
// against third-party services.
// An error will be returned if the route does not define a host.
func (r *Route) GetHostTemplate() (string, error) {
if r.err != nil {
return "", r.err
}
if r.regexp.host == nil {
return "", errors.New("mux: route doesn't have a host")
}
return r.regexp.host.template, nil
}
// GetVarNames returns the names of all variables added by regexp matchersView on GitHub (pinned to db9d1d0073)
Solutions
- Treat the error as 'route accepts all methods' and default to a wildcard.
- Call .Methods(...) on routes that should declare verbs.
- Default to ["*"] or []string{} in docs when it errors.
Example fix
// before
ms, err := route.GetMethods()
// after
ms, err := route.GetMethods()
if err != nil { ms = []string{"*"} } // accepts all Defensive patterns
Strategy: try-catch
Validate before calling
ms, err := route.GetMethods()
if err != nil { ms = []string{"*"} } // accepts all methods Type guard
func hasMethods(rt *mux.Route) bool { _, err := rt.GetMethods(); return err == nil } Try / catch
ms, err := rt.GetMethods()
if err != nil { ms = nil } Prevention
- Always declare Methods on routes meant for docs.
- Treat the error as a wildcard.
- Defensive introspection.
When it happens
Trigger: Calling GetMethods() on a route with no method constraint, e.g. r.HandleFunc("/health", h) without .Methods(...).
Common situations: OpenAPI generators expecting every route to declare verbs; metrics labeling; routes relying on a catch-all handler.
Related errors
- mux: route does not have a path
- mux: route doesn't have queries
- method is not allowed
- no matching route was found
- key not found in metadata
AI-assisted analysis of gorilla/mux@db9d1d0073 (2026-08-04).
Data as JSON: /data/errors/b31fce42032430af.json.
Report an issue: GitHub.