Open gzxy-0102 opened 1 year ago
In This Middleware Can't Get The Path Param
Hello @gzxy-0102, this seems to be working, are you sure this has to do with Middleware? Can you share a code base so we can investigate it further? Thanks.
That's my working example:
package main
import "github.com/kataras/iris/v12"
func main() {
app := iris.New()
app.Get("/api/{mark:uuid}", middleware, handler)
app.Listen(":8080")
}
func middleware(ctx iris.Context) {
mark := ctx.Params().Get("mark") // or .GetString
ctx.Application().Logger().Infof("Mark: %s\n", mark)
ctx.Next()
}
func handler(ctx iris.Context) {
ctx.JSON(iris.Map{
"Mark": ctx.Params().Get("mark"),
})
}
URL: http://localhost:8080/api/359e2e7d-a253-433c-8f14-f92f41467034 Response:
{"Mark":"359e2e7d-a253-433c-8f14-f92f41467034"}
Console screenshot from Middleware log:
this is a example code repo https://github.com/gzxy-0102/iris-issue-2110
Hello @gzxy-0102, this seems to be working, are you sure this has to do with Middleware? Can you share a code base so we can investigate it further? Thanks.
That's my working example:
package main import "github.com/kataras/iris/v12" func main() { app := iris.New() app.Get("/api/{mark:uuid}", middleware, handler) app.Listen(":8080") } func middleware(ctx iris.Context) { mark := ctx.Params().Get("mark") // or .GetString ctx.Application().Logger().Infof("Mark: %s\n", mark) ctx.Next() } func handler(ctx iris.Context) { ctx.JSON(iris.Map{ "Mark": ctx.Params().Get("mark"), }) }
URL: http://localhost:8080/api/359e2e7d-a253-433c-8f14-f92f41467034 Response:
{"Mark":"359e2e7d-a253-433c-8f14-f92f41467034"}
Console screenshot from Middleware log:
Hello @gzxy-0102,
I can't run your example without instructions as it fails to build and run. Can you please point me to the exact file:line that your middleare doesn't display the path parameters and also re-write your project so I can run it? Thank you a lot!
Hello @gzxy-0102,
I can't run your example without instructions as it fails to build and run. Can you please point me to the exact file:line that your middleare doesn't display the path parameters and also re-write your project so I can run it? Thank you a lot!
I've fixed an import bug and added some brief instructions
the middleware is register by Party.UseRouter
, instead of Party.Use
.
https://github.com/gzxy-0102/iris-issue-2110/blob/5014f7b8adff7aeada2329598931017c9b550467/app/router.go#L18
the middleware will be called before main router, at this time, ctx.Params()
is empty.
FYI, PartyConfigure(...).Use(middleware) will take no effects. middleware must register before router register.
package main
import (
"github.com/kataras/iris/v12"
"github.com/kataras/iris/v12/core/router"
)
type mark struct{}
func (m *mark) Configure(r router.Party) {
r.Use(middleware) // middleware must be register before every router register call
r.Get("/{mark:uuid}", m.get)
}
func (m *mark) get(ctx iris.Context) {
ctx.JSON(iris.Map{
"Mark": ctx.Params().Get("mark"),
})
}
func main() {
app := iris.New()
app.Get("/", func(ctx iris.Context) {
ctx.JSON(iris.Map{
"Mark": ctx.Params().Get("mark"),
})
})
app.PartyConfigure("/api", new(mark)).UseRouter(middleware) //.Use(middleware) // UseRouter: middleware can't access ctx.Params(), Use: it's too late to register a middleware
app.Listen(":8181")
}
func middleware(ctx iris.Context) {
mark := ctx.Params().Get("mark") // or .GetString
ctx.Application().Logger().Infof("Mark: %s\n", mark)
ctx.Next()
}
In The Controller In The Middleware Result