Cannot use cors default as type gin.HandlerFunc in argument to router.Use
How do one use this package with gin-gonic?
I have this error: cannot use cors.Default() (type *cors.Cors) as type gin.HandlerFunc in argument to r.Use. My snippet below:
r := gin.Default() r.Use(cors.Default()) v1 := r.Group("/v1") { item := v1.Group("/item") { item.POST("/new", controllers.AddItem) item.GET("/", controllers.GetAllItems) item.GET("/:id", controllers.GetItem) item.PUT("/update", controllers.UpdateItem) item.DELETE("/", controllers.DeleteItem) } verify := v1.Group("/verify") { verify.POST("/", controllers.VerifyItem) } } return r
did you import cors "github.com/rs/cors/wrapper/gin" ?
Also, would really help if you put the snippet with markdown code...
did you import
cors "github.com/rs/cors/wrapper/gin"? Also, would really help if you put the snippet with markdown code...
Yes I did.
r := gin.Default()
r.Use(cors.Default())
v1 := r.Group("/v1")
{
item := v1.Group("/item")
{
item.POST("/new", controllers.AddItem)
item.GET("/", controllers.GetAllItems)
item.GET("/:id", controllers.GetItem)
item.PUT("/update", controllers.UpdateItem)
item.DELETE("/", controllers.DeleteItem)
}
verify := v1.Group("/verify")
{
verify.POST("/", controllers.VerifyItem)
}
}
return r
Please include the imports you are using in your snippet.
@emmanuelbenson did you try putting:
r.Use(cors.Default())
to the last (before return):
r.Use(cors.Default())
return r
This is likely due to no OPTIONS routes defined for the router, gin will just return 404. But if we put the cors into the bottom (i.e. the last one in the router middleware stack), it will catch all unhandled request (and generate a cors response).
The error is -
cannot use "github.com/rs/cors/wrapper/gin".AllowAll() (type "github.com/gin-gonic/gin".HandlerFunc) as type "github.com/<your workspace>/vendor/github.com/gin-gonic/gin".HandlerFunc in argument to router.Use
This is due to your dependencies not being updated
go get -u github.com/rs/cors/wrapper/gin
godep save
and this should fix the above issue
@rs This issue could likely be closed.