{"id":"6ee279fed3887021","repo":"gin-gonic/gin","slug":"http-method-httpmethod-is-not-valid","errorCode":null,"errorMessage":"http method ${httpMethod} is not valid","messagePattern":"http method (.+?) is not valid","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"critical","filePath":"routergroup.go","lineNumber":105,"sourceCode":"\tabsolutePath := group.calculateAbsolutePath(relativePath)\n\thandlers = group.combineHandlers(handlers)\n\tgroup.engine.addRoute(httpMethod, absolutePath, handlers)\n\treturn group.returnObj()\n}\n\n// Handle registers a new request handle and middleware with the given path and method.\n// The last handler should be the real handler, the other ones should be middleware that can and should be shared among different routes.\n// See the example code in GitHub.\n//\n// For GET, POST, PUT, PATCH and DELETE requests the respective shortcut\n// functions can be used.\n//\n// This function is intended for bulk loading and to allow the usage of less\n// frequently used, non-standardized or custom methods (e.g. for internal\n// communication with a proxy).\nfunc (group *RouterGroup) Handle(httpMethod, relativePath string, handlers ...HandlerFunc) IRoutes {\n\tif matched := regEnLetter.MatchString(httpMethod); !matched {\n\t\tpanic(\"http method \" + httpMethod + \" is not valid\")\n\t}\n\treturn group.handle(httpMethod, relativePath, handlers)\n}\n\n// POST is a shortcut for router.Handle(\"POST\", path, handlers).\nfunc (group *RouterGroup) POST(relativePath string, handlers ...HandlerFunc) IRoutes {\n\treturn group.handle(http.MethodPost, relativePath, handlers)\n}\n\n// GET is a shortcut for router.Handle(\"GET\", path, handlers).\nfunc (group *RouterGroup) GET(relativePath string, handlers ...HandlerFunc) IRoutes {\n\treturn group.handle(http.MethodGet, relativePath, handlers)\n}\n\n// DELETE is a shortcut for router.Handle(\"DELETE\", path, handlers).\nfunc (group *RouterGroup) DELETE(relativePath string, handlers ...HandlerFunc) IRoutes {\n\treturn group.handle(http.MethodDelete, relativePath, handlers)\n}","sourceCodeStart":87,"sourceCodeEnd":123,"githubUrl":"https://github.com/gin-gonic/gin/blob/34dac209ffb6ef85cc78c5d217bbb7ad001d68fd/routergroup.go#L87-L123","documentation":"RouterGroup.Handle (routergroup.go:105) panics when the httpMethod argument does not match ^[A-Z]+$ — i.e. it must be non-empty, ASCII, uppercase letters only. This is a startup-time validation because routes are registered at boot; lowercase or non-standard methods are rejected.","triggerScenarios":"Calling router.Handle(\"get\", \"/\", h) (lowercase); router.Handle(\"GET \", ...) (trailing space); router.Handle(\"PATCH;X\", ...) (non-letter); router.Handle(\"\", ...) (empty); using a custom method with a hyphen like \"FOO-BAR\".","commonSituations":"Lowercasing the method via strings.ToLower somewhere; passing a method from an untrusted source; copy-paste from RFC examples that use lowercase; using WebDAV methods with mixed case.","solutions":["Use the canonical http.Method* constants (http.MethodGet, http.MethodPost, ...) instead of string literals.","Upper-case and validate any method coming from configuration: method = strings.ToUpper(method); check with regexp/ASCII before calling Handle.","For non-standard but valid uppercase methods (e.g. WebDAV COPY, LOCK), pass them in uppercase."],"exampleFix":"// before\nrouter.Handle(\"get\", \"/users\", h)\n// after\nrouter.Handle(http.MethodGet, \"/users\", h)","handlingStrategy":"validation","validationCode":"if !regexp.MustCompile(`^[A-Z]+$`).MatchString(method) {\n    log.Fatalf(\"invalid HTTP method %q for route %s\", method, path)\n}\nrouter.Handle(method, path, h)","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Always pass http.MethodGet/Post/Put/Patch/Delete/etc constants to router.Handle.","Upper-case and validate any method coming from config.","Cover every router.Handle call with a startup smoke test."],"tags":["router","http-method","startup","panic","go"],"analyzedSha":"34dac209ffb6ef85cc78c5d217bbb7ad001d68fd","analyzedAt":"2026-08-04T21:26:18.438Z","schemaVersion":2}