猿问

如何在中间件 go-chi 中获取 url 参数

我为特定的路由集使用特定的中间件


r.Route("/platform", func(r chi.Router) {

    r.Use(authService.AuthMiddleware)

    r.Get("/{id}/latest", RequestPlatformVersion)

})

现在我如何访问这个中间件中id的 url 参数AuthMiddleware


func (s *Service) AuthMiddleware(h http.Handler) http.Handler {

    fn := func(w http.ResponseWriter, r *http.Request) {

        fmt.Println(chi.URLParam(r, "id"))

        id := chi.URLParam(r, "id")

        

        if id > 100 {

          http.Error(w, errors.New("Error").Error(), http.StatusUnauthorized)

          return

        }

    }

    return http.HandlerFunc(fn)

}

但是,即使正在运行中间件并且正在调用特定路由,id 参数也会打印为空字符串


慕无忌1623718
浏览 164回答 1
1回答

潇潇雨雨

你把你chi.URLParam的路径参数放在前面{id},你忘了放在.ServeHTTP(w, r)中间件上。如果你不放那个东西,你的请求就不会进入路由内部的路径。这是工作示例:package mainimport (&nbsp; &nbsp; "fmt"&nbsp; &nbsp; "net/http"&nbsp; &nbsp; "github.com/go-chi/chi")func AuthMiddleware(h http.Handler) http.Handler {&nbsp; &nbsp; fn := func(w http.ResponseWriter, r *http.Request) {&nbsp; &nbsp; &nbsp; &nbsp; fmt.Println(chi.URLParam(r, "id"))&nbsp; &nbsp; &nbsp; &nbsp; h.ServeHTTP(w, r)&nbsp; &nbsp; }&nbsp; &nbsp; return http.HandlerFunc(fn)}func main() {&nbsp; &nbsp; r := chi.NewRouter()&nbsp; &nbsp; r.Route("/platform/{id}", func(r chi.Router) {&nbsp; &nbsp; &nbsp; &nbsp; r.Use(AuthMiddleware)&nbsp; &nbsp; &nbsp; &nbsp; r.Get("/latest", func(rw http.ResponseWriter, r *http.Request) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fmt.Println("here ", chi.URLParam(r, "id")) // <- here&nbsp; &nbsp; &nbsp; &nbsp; })&nbsp; &nbsp; })&nbsp; &nbsp; http.ListenAndServe(":8080", r)}我将 移动{id}到platform/{id}以便中间件获得id路径值,并h.ServeHTTP(w, r)在中间件中添加。尝试访问http://localhost:8080/platform/1/latest输出将是:1here&nbsp; 1更新在代码之后运行验证是不好的,你必须修复你定义路径的方式,并.ServeHTTP在验证之后移动。这是一个例子:package mainimport (&nbsp; &nbsp; "errors"&nbsp; &nbsp; "fmt"&nbsp; &nbsp; "net/http"&nbsp; &nbsp; "strconv"&nbsp; &nbsp; "github.com/go-chi/chi")func AuthMiddleware(h http.Handler) http.Handler {&nbsp; &nbsp; fn := func(w http.ResponseWriter, r *http.Request) {&nbsp; &nbsp; &nbsp; &nbsp; fmt.Printf("Middleware First, id: %+v\n", chi.URLParam(r, "id"))&nbsp; &nbsp; &nbsp; &nbsp; id, _ := strconv.Atoi(chi.URLParam(r, "id"))&nbsp; &nbsp; &nbsp; &nbsp; if id > 100 {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; http.Error(w, errors.New("Error").Error(), http.StatusUnauthorized)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; h.ServeHTTP(w, r)&nbsp; &nbsp; }&nbsp; &nbsp; return http.HandlerFunc(fn)}func main() {&nbsp; &nbsp; r := chi.NewRouter()&nbsp; &nbsp; // This works too ()&nbsp; &nbsp; // r.Route("/platform/{id}", func(r chi.Router) {&nbsp; &nbsp; //&nbsp; r.Use(AuthMiddleware)&nbsp; &nbsp; //&nbsp; r.Get("/latest", func(rw http.ResponseWriter, r *http.Request) {&nbsp; &nbsp; //&nbsp; &nbsp; &nbsp; fmt.Println("second: ", chi.URLParam(r, "id")) // <- here&nbsp; &nbsp; //&nbsp; })&nbsp; &nbsp; // })&nbsp; &nbsp; // Other Solution (Wrapping Middleware)&nbsp; &nbsp; r.Route("/platform", func(r chi.Router) {&nbsp; &nbsp; &nbsp; &nbsp; r.Get("/{id}/latest", AuthMiddleware(http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fmt.Println("second: ", chi.URLParam(r, "id")) // <- here&nbsp; &nbsp; &nbsp; &nbsp; })).ServeHTTP)&nbsp; &nbsp; })&nbsp; &nbsp; http.ListenAndServe(":8080", r)}
随时随地看视频慕课网APP

相关分类

Go
我要回答