gofiber/fiber · critical
add: invalid http method %s
Error message
add: invalid http method %s
What it means
Raised by App.register (router.go:1014) when a method string is neither the pseudo-method "USE" nor resolvable by App.methodInt. methodInt returns -1 for any token not in the built-in set (GET, HEAD, POST, PUT, DELETE, CONNECT, OPTIONS, TRACE, PATCH, QUERY), or — when Config.RequestMethods is customized — not present in app.config.RequestMethods (the slices.Index path at helpers.go:1243). App.Add, App.All, Group, Mount and Domain registration all funnel into register, so any of them can trip this guard. The check runs at route-registration time, so the process panics during startup before it serves traffic.
Source
Thrown at router.go:1014
}
pathPretty := pathRaw
if !app.config.CaseSensitive {
pathPretty = utilsstrings.ToLower(pathPretty)
}
if !app.config.StrictRouting && len(pathPretty) > 1 {
pathPretty = utils.TrimRight(pathPretty, '/')
}
pathClean := RemoveEscapeChar(pathPretty)
parsedRaw := parseRoute(pathRaw, app.config.RegexHandler, app.customConstraints...)
parsedPretty := parseRoute(pathPretty, app.config.RegexHandler, app.customConstraints...)
isMount := group != nil && group.app != app
for _, method := range methods {
method = utilsstrings.ToUpper(method)
if method != methodUse && app.methodInt(method) == -1 {
panic(fmt.Sprintf("add: invalid http method %s\n", method))
}
isUse := method == methodUse
isStar := pathClean == "/*"
isRoot := pathClean == "/"
route := Route{
use: isUse,
mount: isMount,
star: isStar,
root: isRoot,
caseSensitive: app.config.CaseSensitive,
path: pathClean,
routeParser: parsedPretty,
Params: parsedRaw.params,
group: group,
View on GitHub (pinned to 9a4c7e57fe)
Solutions
- Pass the fiber.Method* constants (MethodGet, MethodPost, …) instead of hand-typed strings so spelling is guaranteed.
- For custom verbs, declare them in fiber.Config.RequestMethods before registering routes that use them.
- If the method originates from dynamic/untrusted input, validate it against an allow-list before calling Add and reject with 405 rather than registering.
- Inspect the methods slice for empty strings or stray whitespace — ToUpper normalizes case but does not trim spaces.
Example fix
// before — typo panics at boot
app.Add([]string{"POSTT"}, "/users", h)
// after — use the constant
app.Add([]string{fiber.MethodPost}, "/users", h) Defensive patterns
Strategy: validation
Validate before calling
var allowedMethods = map[string]struct{}{
fiber.MethodGet: {}, fiber.MethodHead: {}, fiber.MethodPost: {}, fiber.MethodPut: {},
fiber.MethodPatch: {}, fiber.MethodDelete: {}, fiber.MethodConnect: {},
fiber.MethodOptions: {}, fiber.MethodTrace: {}, fiber.MethodQuery: {},
}
// add any custom methods declared in Config.RequestMethods
for _, m := range app.Config().RequestMethods {
allowedMethods[m] = struct{}{}
}
for _, m := range methods {
if _, ok := allowedMethods[strings.ToUpper(m)]; !ok {
return fmt.Errorf("rejecting registration: unsupported method %q", m)
}
}
app.Add(methods, "/x", h) Try / catch
defer func() {
if r := recover(); r != nil {
log.Printf("route registration failed: %v", r)
// do not re-register; surface the bad method to the caller
}
}()
app.Add(methods, "/x", h) Prevention
- Always pass fiber.Method* constants instead of string literals.
- Validate any method originating from config, headers, or c.Method() against an allow-list before Add.
- When customizing Config.RequestMethods, declare every custom verb there before registering it.
- Add a unit test that exercises each registration entry point to catch bad methods at CI time, not boot time.
When it happens
Trigger: Call app.Add([]string{"FOO"}, "/x", h) or pass a typo such as "GETT"/"POSTT", an empty string (ToUpper yields ""), or a custom method name that was never added to fiber.Config.RequestMethods. Also reachable via App.All, group.Get-like helpers that derive methods dynamically, or Mount/Domain paths — all invoke register and hit line 1013-1014.
Common situations: Typos in method string literals; passing a runtime-derived method (from config, c.Method(), or a header) into Add without validation; assuming non-RFC verbs like PURGE/LINK/UNLINK are supported out of the box; or customizing Config.RequestMethods and then forgetting to declare a method there before registering it (note: once RequestMethods is non-empty, methodInt uses slices.Index over it, so even GET must be listed).
Related errors
- favicon: read limited: %w
- favicon: file size exceeds max bytes %d
- logger: RegisterContextTag requires a non-empty name and ext
- missing handler/middleware in route: %s
- runtime.Goexit() called in handler or server panic
AI-assisted analysis of gofiber/fiber@9a4c7e57fe (2026-08-04).
Data as JSON: /data/errors/ca3555c19bffa8bc.json.
Report an issue: GitHub.