You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Describe the bug
Using func (r *Route) Methods(methods ...string) more than once for a single route results in a response with status code 405 Method Not Allowed for this route.
Background
I tried to simplify the development of my web APIs, so I introduced some utility functions, with the relevant part essentially being:
Response with status code 200 (for the latter example).
Solutions?
Either:
Disallow using Methods(...) more than once, e.g. via changing field routeConf.matchers from type []matcher to type map[string]matcher, where key string (or any other practical type) may be "METHODS".
Force method matchers to be unique and to be merged if it already exists, e.g. as per above.
Change func (r *Route) Match(req *http.Request, match *RouteMatch) to not return ErrMethodMismatch if any method does not match, i.e. if there are multiple method matchers.
It has been not clear to me that using Methods(...) twice leads to this behaviour (hence this issue), so I would at least appreciate some kind of information at the function description in the source file -- although I do know now.
Also, I neither fully understand your intended design nor Git things like Pull Requests, so I am sorry if my provided solutions may very well be rather lacking.
The text was updated successfully, but these errors were encountered:
Just for understanding, I have tried to write a basic generic minimal code to understand what happens when we call Methods multiple times.
// You can edit this code!// Click here and start typing.package main
import"fmt"typematcherinterface{}
funcmain() {
str:= []matcher{"GET"}
str=append(str, matcher([]string{"PUT"}))
fmt.Println(str)
}
Output of the above code is
[GET [PUT]]
Similarly in mux while matching the HTTP method [PUT] is not a valid method thus returns 405 not allowed.
I hope I answered your queries and doubts @edgy-sphere
Thank you!
tebruno99
changed the title
[bug] Using route.Methods(...) more than once
[bug] Chaining route.Methods(...) more than once causes confusion
Dec 15, 2022
MIGRATED
From mux created by edgy-sphere: gorilla#694
Describe the bug
Using
func (r *Route) Methods(methods ...string)
more than once for a single route results in a response with status code405 Method Not Allowed
for this route.Background
I tried to simplify the development of my web APIs, so I introduced some utility functions, with the relevant part essentially being:
(
OPTIONS
is basically always required;methods
as a slice becausePUT
,PATCH
and evenPOST
may point to the same handler)Versions
go version go1.19 windows/amd64
Steps to Reproduce
(GitHub repo)
Expected behavior
Response with status code 200 (for the latter example).
Solutions?
Either:
Methods(...)
more than once, e.g. via changing fieldrouteConf.matchers
from type[]matcher
to typemap[string]matcher
, where keystring
(or any other practical type) may be"METHODS"
.func (r *Route) Match(req *http.Request, match *RouteMatch)
to not returnErrMethodMismatch
if any method does not match, i.e. if there are multiple method matchers.It has been not clear to me that using
Methods(...)
twice leads to this behaviour (hence this issue), so I would at least appreciate some kind of information at the function description in the source file -- although I do know now.Also, I neither fully understand your intended design nor Git things like Pull Requests, so I am sorry if my provided solutions may very well be rather lacking.
The text was updated successfully, but these errors were encountered: