Case insensitive paths?

645 views
Skip to first unread message

Marc Harter

unread,
Jun 29, 2016, 12:24:49 PM6/29/16
to Gorilla web toolkit
Is there a way to do this in Gorilla Mux?  We recently moved our API to Mux and discovered some of our users were not using all lowercase in their paths and would need to support.

E.g.

r.HandleFunc("/v1/site/{site}/order", Upload)

Would like that to match things like /v1/site/{site}/Order?

Thanks,
Marc

Matt Silverlock

unread,
Jun 29, 2016, 4:28:20 PM6/29/16
to Gorilla web toolkit
Two approaches:
  • If you don't need to maintain the upper-cased route, write middleware that wraps your router and lower-cases the path component of r.URL (see example)
  • You can use regex matches as well.
e.x.

func main() {
r := mux.NewRouter()
r.HandleFunc("/route", Handler)

log.Fatal(http.ListenAndServe("localhost:8000", LowerCaseURI(r)))
}

func Handler(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("hello\n"))
}

func LowerCaseURI(h http.Handler) http.Handler {
fn := func(w http.ResponseWriter, r *http.Request) {
r.URL.Path = strings.ToLower(r.URL.Path)
h.ServeHTTP(w, r)
}

return http.HandlerFunc(fn)
}

Marc Harter

unread,
Jun 30, 2016, 10:30:24 AM6/30/16
to goril...@googlegroups.com
Thanks Matt, the LowerCaseURI handler should work great for us!

--
You received this message because you are subscribed to a topic in the Google Groups "Gorilla web toolkit" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/gorilla-web/rpwSDVOxkyc/unsubscribe.
To unsubscribe from this group and all its topics, send an email to gorilla-web...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Mihai Chiorean

unread,
Apr 2, 2019, 2:47:25 PM4/2/19
to Gorilla web toolkit



      You might want to do `req.URL.RawQuery= strings.ToLower(req.URL.RawQuery)` too, otherwise if case is wrong on query param, you'll get 404s
To unsubscribe from this group and all its topics, send an email to goril...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages