gogorilla

How do I print the url path without parameters?


I have a RESTful API using Gorilla/mux. It is parameterized in the path. I am creating it as such:

r := mux.NewRouter()
subr := r.PathPrefix("/v1").Subrouter()
subr.Handle("/organizations/{orgId}/projects/{projectId}", CreateProject()).Methods(http.MethodPost)

However, when capturing the request and logging the outcome, I don't want to log i.e.

/organizations/fff-555-aaa9999/projects/amazing-project

which is what I'd get from parsing the r *http.Request.URL value.

Instead, I'd like to get the initial router value of /organizations/{orgId}/projects/{projectId} so I can filter from that in my logging.

Using gorilla/mux, is there a way to get the router path without the actual parameters, but the parameter variables instead?


Solution

  • Inside CreateProject function you coud use this:

    func CreateProject(w http.ResponseWriter, r *http.Request) {
        path, _ := mux.CurrentRoute(r).GetPathTemplate()
        fmt.Println(path)
    }