RequestID
RequestID middleware for Fiber that adds an indentifier to the response.

Table of Contents

Signatures

1
func New(config ...Config) fiber.Handler
Copied!

Examples

Import the middleware package that is part of the Fiber web framework
1
import (
2
"github.com/gofiber/fiber/v2"
3
"github.com/gofiber/fiber/v2/middleware/requestid"
4
)
Copied!
After you initiate your Fiber app, you can use the following possibilities:
1
// Default middleware config
2
app.Use(requestid.New())
3
​
4
// Or extend your config for customization
5
app.Use(requestid.New(requestid.Config{
6
Header: "X-Custom-Header",
7
Generator: func() string {
8
return "static-id"
9
},
10
}))
Copied!

Config

1
// Config defines the config for middleware.
2
type Config struct {
3
// Next defines a function to skip this middleware when returned true.
4
//
5
// Optional. Default: nil
6
Next func(c *fiber.Ctx) bool
7
​
8
// Header is the header key where to get/set the unique request ID
9
//
10
// Optional. Default: "X-Request-ID"
11
Header string
12
​
13
// Generator defines a function to generate the unique identifier.
14
//
15
// Optional. Default: utils.UUID
16
Generator func() string
17
​
18
// ContextKey defines the key used when storing the request ID in
19
// the locals for a specific request.
20
//
21
// Optional. Default: requestid
22
ContextKey string
23
}
Copied!

Default Config

1
var ConfigDefault = Config{
2
Next: nil,
3
Header: fiber.HeaderXRequestID,
4
Generator: func() string {
5
return utils.UUID()
6
},
7
ContextKey: "requestid"
8
}
Copied!
Last modified 7mo ago