Skip to content

Trying to obtain RequestId within Request return empty #2266

Answered by aldas
terzano asked this question in Q&A
Discussion options

You must be logged in to vote

I am able see get id with this example:

func main() {
	e := echo.New()
	e.Use(middleware.RequestID())

	e.GET("/", func(c echo.Context) error {
		id := c.Response().Header().Get(echo.HeaderXRequestID)
		return c.String(http.StatusOK, id)
	})

	if err := e.Start(":8080"); err != http.ErrServerClosed {
		e.Logger.Fatal(err)
	}
}

that middleware config has also RequestIDHandler that you can use to set id value to context store

func main() {
	e := echo.New()
	e.Use(middleware.RequestIDWithConfig(middleware.RequestIDConfig{
		RequestIDHandler: func(c echo.Context, s string) {
			c.Set("MY_REQUEST_ID", s)
		},
	}))

	e.GET("/", func(c echo.Context) error {
		id, _ := c.Get("MY_REQUEST_ID").(string

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@terzano
Comment options

Answer selected by terzano
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants