2
votes

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)
1
What is your actual problem? You provided a screenshot, and it is not obvious what's wrong with it. - zerkms
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? - Harshil
"I expect "tenant" to be added as another field" --- and it's not happening? Why do you think so? Show your code that reads from the context. Also what exact value does the tenant variable hold? - zerkms
Btw, there is a special function c.Set(), then c.GetString - zerkms
I spent some more time and it seems that because there was some value added initially in the context, the other one's are now nested as context inside context. I am looking into that now. Updated the question to show how I am trying to access tenant. Looking into c.Set and c.GetString now.Thanks for the headsup - Harshil

1 Answers

4
votes

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")
}