Gin: grouping routes and middleware not working

Created on 13 Feb 2016  路  10Comments  路  Source: gin-gonic/gin

I am trying to add this middleware

func CORS () gin.HandlerFunc {
    return func(context *gin.Context) {
        context.Writer.Header().Add("Access-Control-Allow-Origin", "*")
        context.Writer.Header().Set("Access-Control-Max-Age", "86400")
        context.Writer.Header().Set("Access-Control-Allow-Methods", "POST, GET, OPTIONS, PUT, DELETE, UPDATE")
        context.Writer.Header().Set("Access-Control-Allow-Headers", "Content-Type, Content-Length, Accept-Encoding, X-CSRF-Token, Authorization, accept, origin, Cache-Control, X-Requested-With")
        context.Writer.Header().Set("Access-Control-Expose-Headers", "Content-Length")
        context.Writer.Header().Set("Access-Control-Allow-Credentials", "true")

        if context.Request.Method == "OPTIONS" {
            context.AbortWithStatus(200)
        } else {
            context.Next()
        }
    }
}

but not work with grouping routes

  func main () {
    router := gin.New()
    router.Use(CORS())

    // cors middleware not working
    users := router.Group("/users")
    users.GET("/", Users)

    // cors middleware not working
    users := router.Group("/users")
    users.Use(CORS())
    users.GET("/", Users)

    // cors middleware working fine
    router.GET("/users")

    router.Run(":3000")
  }
question

Most helpful comment

@rasheedhamdawi is this resolved ? anything share ?

All 10 comments

Interesting. I used the following main-function and everything works fine.

What part of your code is not working?

  func main () {
    router := gin.New()
    router.Use(CORS())

    users := router.Group("/users")
    users.GET("/", Users)

    router.Run(":3000")
  }

@rasheedhamdawi is this resolved ? anything share ?

@douglarek I had exactly this problem. You should define your middlewares before your grouping route(maybe right after creating the router instance)

Same issue, and you should put your middleware before the routes, like this, and it works.

    r := gin.Default()
    r.Use(Authentication())
    r.POST("/v2/create", handlers.CreateHandler)

And here it is what I'm using:

transaction := api.Group("/transaction", HandleApiKey())

transaction.GET("/", func(c *gin.Context) {
    //a nice logic will be here
})

func HandleApiKey() gin.HandlerFunc {
    return func(c *gin.Context) {
    //another nice middleware will be here
    }
}

Why is this closed? As far as I can tell, this is still an issue. Or are we not meant to use middleware on router groups?

@jmwohl Can I see the code you are using? Have you defined the middleware before the group route?

If I define your route like this users.GET("/", Users) in api call you I added a leading slash fetch("http://localhost:3000/users/")

I defined the route without leading slash users.GET("", Users) then I get a cors error when I add the leading slash in api request.

I don't think that cors should have anything to do with leading slashes.

This is an issue!
Is impossible to add middleware to Groups

Found the problem it's related to gin-contrib/Cors is mandatory to add the options route on subgroup

Was this page helpful?
0 / 5 - 0 ratings

Related issues

iiinsomnia picture iiinsomnia  路  3Comments

lilee picture lilee  路  3Comments

wangcn picture wangcn  路  3Comments

olegsobchuk picture olegsobchuk  路  3Comments

mastrolinux picture mastrolinux  路  3Comments