Reputation: 8796
I have rest API defined as
apis.GET(/home, validatationHandler , dashboardHandler)
I want pass some data from validatationHandler to dashboardHandler. For this I thought of using header. To set the data I use this in validatationHandler
c.Writer.Header().Set("myheader", "mytoken")
c.Next()
and in dashboardHandler I tried to access it using
fmt.Println(c.Request.Header.Get("myheader"))
But the value is always nil. Any idea how can I set and retrieve headers? Is there any other way I can pass on the data from 1 handler to another?
Upvotes: 0
Views: 1279
Reputation: 3163
You can pass values via gin.Context
Use ctx.Set(k, v)
in fisrt one and ctx.Get(k)
in the next.
So How to Use It:
ctx.Set("myKey", 100)
and get it using
v, ok := ctx.Get("myKey")
if ok {
actualValue := v.(int) // you need to type convert it as it returns interface.
}
See context.go
Upvotes: 2