iris icon indicating copy to clipboard operation
iris copied to clipboard

Get Path Params In The Middleware

Open gzxy0102 opened this issue 2 years ago • 6 comments

In The Controller image In The Middleware image Result image

gzxy0102 avatar Mar 27 '23 07:03 gzxy0102

In This Middleware Can't Get The Path Param

gzxy0102 avatar Mar 27 '23 08:03 gzxy0102

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:

image

kataras avatar Apr 06 '23 12:04 kataras

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:

image

gzxy0102 avatar Apr 07 '23 04:04 gzxy0102

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!

kataras avatar Apr 25 '23 19:04 kataras

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

gzxy0102 avatar Apr 26 '23 02:04 gzxy0102

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()
}

zxfishhack avatar Oct 21 '23 16:10 zxfishhack