Harshil
Harshil

Reputation: 484

How to set data in gin request context?

I have a very simple piece of code. One single middleware is applied to all routes. In this middleware, the header field 'x-sentinel-tenant' is read. If it is blank, an error is returned. If it has some value, then that value is to be extracted and set in the request context for later use.

Here's my middleware code

// VerifyTenant ensures that the user has added tenant information in the header
func VerifyTenant(c *gin.Context) {
    requestTenant := c.Request.Header.Get("x-tenant")
    if requestTenant == "" {
        c.AbortWithStatusJSON(
            http.StatusBadRequest,
            views.GenerateErrorResponse(
                http.StatusBadRequest,
                "Please add tenant in the header.",
                c.Request.URL.Path,
            ),
        )
        return
    }
    c.Request = c.Request.WithContext(context.WithValue(c, tenant, requestTenant))
}

When I try to access this context in a later part of my code, here's how I get the data when hovered over the ctx value.

Please notice how there's a context within my context which is absurd

I am not sure what am I doing wrong. It's a fairly straight forward piece of code which is behaving very funny. Any leads will be appreciated. Thank you

When I update the context, I expect "tenant" to be added as another field. Which is not happening. Can multiple values not coexist in the same context?

My question - how to correctly set multiple values in gin request context Here's how I am trying to access tenant

return ctx.Value("Context").(context.Context).Value("tenant").(string)

Upvotes: 13

Views: 38978

Answers (1)

Arith
Arith

Reputation: 416

https://pkg.go.dev/github.com/gin-gonic/gin?tab=doc#Context.Set

func (*Context) Set does exactly what you want. an example is:

func VerifyTenant(ctx *gin.Context) {
    requestTenant := ctx.GetHeader("x-tenant")
    if requestTenant == "" {
        // Abort
        return
    }
    ctx.Set("x-tenant", requestTenant)
}

func Test(ctx *gin.Context){
    // when you need to get the value of "x-tenant"

    requestTenant := ctx.GetString("x-tenant")
}

Upvotes: 20

Related Questions