-
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
[question] How to get the Url without Path Params #669
Comments
@vijay8059 I don't know what is your purpose or what means "get the url". However, in server side, you can get the request URL with http.Request.URL. |
@ya5u i am pushing this metrics to prometheus it's become difficult to identify the url . in the prometheus it's showing as different endpoints |
In the handlers you can retrieve the URL field from the *http.Request object of the handlers function parameters (usually named Example: func ArticlesCategoryHandler(w http.ResponseWriter, r *http.Request) {
fmt.Println(r.URL)
} |
@vijay8059 This library can get the URL without matching the path:
|
@vijay8059 can you explain use case? In general I would highly recommend you to go through the link My 2 cents would be to create nested routes: func ProductsHandler( w http.ResponseWriter, r *http.Request) bool {
fmt.Println(r.URL.Path)
}
func ProductsHandler( w http.ResponseWriter, r *http.Request) bool {
....
}
func ProductDetailsHandler( w http.ResponseWriter, r *http.Request) bool {
....
}
r := mux.NewRouter()
s := r.PathPrefix("/v1/random").Subrouter()
// "/v1/random"
s.HandleFunc("/", ProductsHandler)
// "/v1/random/{id}"
s.HandleFunc("/{id}/", ProductHandler)
// "/v1/random/{id}/details"
s.HandleFunc("/{id}/details", ProductDetailsHandler) |
I'm looking for something similar. I have a tracing middleware and want to set the operation name to the route that was matched for a request. Using your example (simplified):
I want a middleware that looks like this:
Using the default mux from the go library, you can do this:
IMO, that |
Essentially, given an |
Describe the problem you're having
…
Versions
…
"Show me the code!"
…
The text was updated successfully, but these errors were encountered: