0
votes

I'm using Gorilla mux in my golang api for routing. I have two paths that are similar: /users/{id} and /users/settings. When I make a call to the /users/settings endpoint, it is getting routed to the endpoint /users/{id}. How do I fix this?

router := mux.NewRouter()
router.HandleFunc("/users/{id}", usersController.GetUserDetail).Methods(http.MethodGet)
router.HandleFunc("/users/settings", usersController.GetSettings).Methods(http.MethodGet)
2
Use a regular expression for idBurak Serdar
Register /settings first.Peter

2 Answers

1
votes

Use regexp for route like id /{id:[0-9]+} it will match digits.

0
votes

Using the solution Gorilla MUX routing with similar paths. I switched the ordering and now I am registering the settings first.

router.HandleFunc("/users/settings", usersController.GetSettings).Methods(http.MethodGet)

router.HandleFunc("/users/{id}", usersController.GetUserDetail).Methods(http.MethodGet)