kataras / iris

The fastest HTTP/2 Go Web Framework. New, modern and easy to learn. Fast development with Code you control. Unbeatable cost-performance ratio :rocket:
https://www.iris-go.com
BSD 3-Clause "New" or "Revised" License
25.29k stars 2.47k forks source link

Get Path Params In The Middleware #2110

Open gzxy-0102 opened 1 year ago

gzxy-0102 commented 1 year ago

In The Controller image In The Middleware image Result image

gzxy-0102 commented 1 year ago

In This Middleware Can't Get The Path Param

kataras commented 1 year ago

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

gzxy-0102 commented 1 year ago

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

kataras commented 1 year ago

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!

gzxy-0102 commented 1 year ago

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

zxfishhack commented 1 year ago

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