Skip to content

How to parse value from (echo.context).Get ? #2312

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

You must be logged in to vote

You need to cast into correct type

value, ok := c.Get("KEY").(time.Time)
if !ok {
	return errors.New("invalid type for KEY")
}

NB: if you set it as pointer you need to cast it back to pointer. Or if you set value into context and its type is pointer to token.Payload you need to cast it back to *token.Payload when you Get it from token. api.Payload is different type/struct I assume.

or longer example:

func main() {
	e := echo.New()

	e.Use(func(next echo.HandlerFunc) echo.HandlerFunc {
		return func(c echo.Context) error {
			c.Set("KEY", time.Now())
			return next(c)
		}
	})

	e.GET("/", func(c echo.Context) error {
		value, ok := c.Get("KEY").(time.Time)
		if !ok {
			return errors.New("…

Replies: 1 comment 1 reply

Comment options

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

Answer selected by topben
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