gofiber/fiber · error

add: invalid http method

Error message

add: invalid http method %s

What it means

register upper-cases each HTTP method and looks it up via app.methodInt. If the method is neither the special "USE" token nor one of the recognized HTTP methods (GET, HEAD, POST, PUT, DELETE, CONNECT, OPTIONS, TRACE, PATCH, QUERY — or the custom set configured via App.Config.RequestMethods), it panics with "add: invalid http method <method>". This guards the router's method-indexed stack against unknown verbs at startup.

Solutions

  1. Use the fiber.Method* constants (fiber.MethodGet, fiber.MethodPost, etc.) instead of string literals.
  2. If methods come from config, validate them against an allowlist before registering.
  3. For genuinely custom HTTP verbs, register them via fiber.Config.RequestMethods when creating the App, so methodInt recognizes them.

Example fix

// before
app.Add("GETT", "/x", h) // typo, panics

// after
app.Add(fiber.MethodGet, "/x", h)
Defensive patterns

Strategy: validation

Validate before calling

var allowedMethods = map[string]bool{
    fiber.MethodGet: true, fiber.MethodHead: true, fiber.MethodPost: true,
    fiber.MethodPut: true, fiber.MethodDelete: true, fiber.MethodConnect: true,
    fiber.MethodOptions: true, fiber.MethodTrace: true, fiber.MethodPatch: true,
    fiber.MethodQuery: true,
}
func validateMethod(method string) error {
    if !allowedMethods[strings.ToUpper(method)] {
        return fmt.Errorf("invalid HTTP method %q", method)
    }
    return nil
}

Type guard

func isValidHTTPMethod(method string) bool {
    _, ok := allowedMethods[strings.ToUpper(method)]
    return ok
}

Prevention

When it happens

Trigger: app.Add("GE", "/x", h) (typo); app.Add("FOO", "/x", h); app.Get handled correctly but a dynamic app.Add(methodFromConfig, ...) where methodFromConfig is an arbitrary string.

Common situations: A typo in a method constant; a method string sourced from config or an external system that contains an invalid verb; case issues are NOT the cause (the value is upper-cased first) but whitespace or surrounding characters are; using a custom method without first registering it in Config.RequestMethods.

Related errors


AI-assisted analysis of gofiber/fiber@a105acad6c (2026-08-11). Data as JSON: /api/errors/ca3555c19bffa8bc. Report an issue: GitHub.

Appendix: source

Thrown at router.go:1031

	}
	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 a105acad6c)