-
Notifications
You must be signed in to change notification settings - Fork 1.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Optionally Match trailing slash in the URL and HTTP Method based matching #30
Comments
For the latter you need to make two routes, and use eg:
If you want a route to match both with and without a trailing slash, but not have any redirection, you just have to make two routes with the same handler. |
You can also use http://godoc.org/github.com/gorilla/mux#Router.StrictSlash |
Also in the future you can ask questions here instead of filing an issue: https://groups.google.com/forum/#!forum/gorilla-web |
What if I don't want the redirect, but just match the trailing slash optionally? |
+1 to the question from @fbjork. I also want to have this behavior |
FWIW, you can do this using patterns:
will handle |
Is there a way to do this when the last part in your path is a variable, for example if I want the same route to match for |
Does the trailing slash need to be in the regular expression?
If it absolutely must, use an optional non-capturing group to indicate it
as optional only.
…On Tue, Aug 8, 2017 at 5:34 AM Brendan Abolivier ***@***.***> wrote:
FWIW, you can do this using patterns:
m.Handle("/{route:route\\/?}", handler)
will handle /route as well as /route/
Is there a way to do this when the last part in your path is a variable,
for example, if I want the same route to match for /{var} and /{var}/? Or
does it necessary lead to code duplication?
—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
<#30 (comment)>, or mute
the thread
<https://github.com/notifications/unsubscribe-auth/AABIcP7AxZUjp_Bgpuyh_Lb9aj7muiNHks5sWFXAgaJpZM4A1FC_>
.
|
How would you do that in my case? Sorry for asking, I might not be familiar enough with the groups' syntax. I tried a few things but none of them worked. |
e.g.
➜ ~ curl http://localhost:8080/route
Hello
➜ ~ curl http://localhost:8080/route/
Hello If that doesn't make sense, provide an example of what you think the duplicate is. |
My use case is that the last part of the root path is a variable, while your example shows the case of a static last part. I'd like the route to match both |
The second part of the pattern is not really static, it's a regular expression ( package main
import (
"log"
"net/http"
"github.com/gorilla/mux"
)
func main() {
m := mux.NewRouter()
m.HandleFunc("/things/{id:[0-9](?:\\/)?}", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("match"))
})
log.Fatal(http.ListenAndServe(":1337", m))
} This will match |
I understand that (sorry for not being specific enough between static/strict regular expression). However, a regular expression isn't declared the same way a variable is in a route. eg: m.HandleFunc("/things/{thing:[a-z]}", handler) isn't, from what I understand, the same as m.HandleFunc("/things/{thing}", handler) my case is the second one. Sorry if I'm not clear enough. I perfectly understand how to do it with a regular expression, but not how to do it with just the variable name. |
Wouldn't it be simpler to just have a parameter to ignore trailing slashes? |
Any updates on this one? This seems like prime real estate for a simple parameter. |
What would that parameter look like? Each new parameter we add is more API surface for users to learn and understand. This can be achieved via existing pattern support here: #30 (comment) |
@elithrar how would one take care of the situation where the last part of the path is a variable e.g. This will match both From the source code, it seems pretty hard to get around as I believe that the relevant part of the regex expression generated would be |
Ran into this issue. This solution is great and so underrated! Lots of other blog solutions recommend middleware to handle the trailing slash without a redirect (from using Edit to add: you can see options to handle this trailing slash redirect with middleware as an alternative: - |
I honestly think treating the trailing slash as optional should be the default behaviour. And indicating you want to treat the route with a trailing slash different I feel is the much less common use case, and that should be what needs to be turned on. But, if the plan/ideal goal is to stick with current behaviour, what about something like this:
|
If you want
Both routes will now call the apiHandler with |
I think I found a solution to the issue with the trailing slash. Suffix slash is the most annoying thing when the request method is different than Instead of writing complex or inelegant solutions like multiplying the
And wherever your
Hope it helps! |
Lets say we have a URL like
http://www.hostname/upload/
Considering that the user types in
http://www.hostname/upload
(note no trailing slash)Possibly we can redirect the user from the no-slash one to the slashed one by registering different handlers.
StrictSlash
does this. But is there a way to match both of them?we use mux like this
How do we write the matching condition so that it accepts both
http://www.hostname/upload/
andhttp://www.hostname/upload
?Another question. How do we execute different handlers based on the request method?
e.g., the
GET
request to upload can be handled by different handler andPOST
can be handled by another function.I already know that we can check request.Method in the handler and do different things accordingly. But is there a way to do such HTTP method based matching?
The text was updated successfully, but these errors were encountered: