You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
// getProject returns all the projects as an API response
func (s *Server) getProject(w http.ResponseWriter, r *http.Request) {
params := httprouter.ParamsFromContext(r.Context())
log.Println(params)
id, err := strconv.Atoi(params.ByName("id"))
if err != nil {
s.logger.Println(errors.New("invalid movie id : "), err)
utils.ErrorJSON(w, err)
return
}
utils.WriteJSON(w, http.StatusOK, id, "data")
}
Inside getProjects(), r.Context().Value("params") returns [{id 1}] but httprouter.ParamsFromContext(r.Context()) returns [] when I use the s.wrap() call, but if I do not use it then the params are passed properly.
Any pointers?
The text was updated successfully, but these errors were encountered:
What exactly is params in your wrap() function? Please provide a minimal working example to replicate the issue next time.
The issue you are facing is due to the wrong key you are passing as params in the wrap() function. httprouter.ParamsFromContext() expects to find the parameters with the key httprouter.ParamsKey. Here is a working version of your wrap() function:
Using alice with httprouter to chain middleware and wrapping params before passing it to the middleware function.
Inside getProjects(), r.Context().Value("params") returns [{id 1}] but httprouter.ParamsFromContext(r.Context()) returns [] when I use the s.wrap() call, but if I do not use it then the params are passed properly.
Any pointers?
The text was updated successfully, but these errors were encountered: