Skip to content

🤗 [Question]: How to get path parameters in custom middleware? #3323

Closed
@allyxmiko

Description

@allyxmiko

Question Description

I want to use c.ParamsInt to get the path parameter in my custom middleware, but it seems that I cannot get it.

In the code snippet below, the request path is as follows "/api/action/wol/12". I can get 12 through c.ParamsInt in the final handler, but cannot get 12 through c.ParamsInt in the custom middleware.

I am not sure if this is as it should be or a bug.

Code Snippet (optional)

// server.go
func Init() {
	app := fiber.New()

	v1 := app.Group("/api/action")
	v1.Use(middleware.BaseAuth, middleware.SetDeviceId)
	v1.Get("/wol/*", api.GetStatus)
        // ...apis 

	app.Listen(":3000")
}

// api.go
func GetStatus(c *fiber.Ctx) error {
	deviceId, _ := c.ParamsInt("*", 0)
	fmt.Printf("handler: %d\n", deviceId) // output: handler: 12
	return c.JSON(resp.Ok("Hello World"))
}

// middleware.go
func SetDeviceId(c *fiber.Ctx) error {
	deviceId, _ := c.ParamsInt("*", 0) 
	fmt.Printf("middleware: %d\n", deviceId) // output: middleware: 0
	return c.Next()
}

Checklist:

  • I agree to follow Fiber's Code of Conduct.
  • I have checked for existing issues that describe my questions prior to opening this one.
  • I understand that improperly formatted questions may be closed without explanation.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions