Reputation: 53
I currently have my routes set for my REST API as: using mux
localhost:8080/user/{id} localhost:8080/space/{id}
server.Router.HandleFunc("/users", middlewares.SetMiddlewareJSON(server.GetUsers)).Methods("GET")
server.Router.HandleFunc("/posts", middlewares.SetMiddlewareJSON(server.GetPosts)).Methods("GET")
the set middleware function
func SetMiddlewareJSON(next http.HandlerFunc) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
next(w, r)
}
}
How would I combine them to become
localhost:8080/user/1/post/{id}
the repository for the project is here: https://github.com/Robbie-Thomas/fullstack
Upvotes: 0
Views: 204
Reputation: 51542
You will need a HandlerFunc
with that path:
server.Router.HandleFunc("/users/{userId}/post/{id}", middlewares.SetMiddlewareJSON(server.GetUsers)).Methods("GET")
In the handler func, you'll access those variables:
vars:=mux.Vars(request)
userId:=vars["userId"]
postId:=vars["id"]
Upvotes: 3