Params
Method can be used to get the route parameters, you could pass an optional default value that will be returned if the param key does not exist.
Defaults to empty string (""
), if the param doesn’t exist.
func (c *Ctx) Params(key string, defaultValue ...string) string
// GET http://example.com/user/fenny
app.Get("/user/:name", func(c *fiber.Ctx) error {
c.Params("name") // "fenny"
// ...
})
// GET http://example.com/user/fenny/123
app.Get("/user/*", func(c *fiber.Ctx) error {
c.Params("*") // "fenny/123"
c.Params("*1") // "fenny/123"
// ...
})
Unnamed route parameters(*, +) can be fetched by the character and the counter in the route.
// ROUTE: /v1/*/shop/*
// GET: /v1/brand/4/shop/blue/xs
c.Params("*1") // "brand/4"
c.Params("*2") // "blue/xs"
For reasons of downward compatibility, the first parameter segment for the parameter character can also be accessed without the counter.
app.Get("/v1/*/shop/*", func(c *fiber.Ctx) error {
c.Params("*") // outputs the values of the first wildcard segment
})
Returned value is only valid within the handler. Do not store any references.
Make copies or use theImmutable
setting instead. Read more…__