Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
3 tasks done
allyxmiko opened this issue Feb 24, 2025 · 3 comments
Closed
3 tasks done

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

allyxmiko opened this issue Feb 24, 2025 · 3 comments

Comments

@allyxmiko
Copy link

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.
Copy link

welcome bot commented Feb 24, 2025

Thanks for opening your first issue here! 🎉 Be sure to follow the issue template! If you need help or want to chat with us, join us on Discord https://gofiber.io/discord

@ReneWerner87
Copy link
Member

#1463 (comment)

behavior is expected

the handler where you try to extract the parameter has no parameters defined in the route

@allyxmiko
Copy link
Author

@ReneWerner87 Thank you for your answer!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants