Hi, I need your help. After hours searching how expose cors middleware I haven't found any about it (different to my config).
I expose some middlewares in my main.go file
//Start default router
r := gin.Default()
// Health route
r.Group("/").GET("/health", c.Health)
// Subpath
routes := r.Group("/v1")
// We get the config from consul
jwtParser := jwt.NewParser(consul.ConsulConfig.GetJwtParserConfig())
// CORS
routes.Use(auth.CORSMiddleware())
// Every route it will be analized by this middleware
routes.Use(auth.Middleware(jwtParser))
In the CORSMiddleware I have the following code:
func CORSMiddleware() gin.HandlerFunc {
return func(c *gin.Context) {
c.Writer.Header().Set("Access-Control-Allow-Origin", "*")
c.Writer.Header().Set("Access-Control-Allow-Credentials", "true")
c.Writer.Header().Set("Access-Control-Allow-Headers", "Content-Type, Content-Length, Accept-Encoding, X-CSRF-Token, Authorization, accept, origin, Cache-Control, X-Requested-With")
c.Writer.Header().Set("Access-Control-Allow-Methods", "POST, HEAD, PATCH, OPTIONS, GET, PUT")
if c.Request.Method == "OPTIONS" {
c.AbortWithStatus(204)
return
}
c.Next()
}
}
I am developing a website that I need to make some gets and posts to my dev go server, I send some requests from localhost:4200 and I get an error..
Access to XMLHttpRequest at 'https://xxxxxx/v1/xxxx/usage' from origin 'http://localhost:4200' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: Redirect is not allowed for a preflight request.
zone.js:3382 POST https://xxxxxx/v1/xxxx/usage net::ERR_FAILED
Is that line in the cors middleware not working? Or I missing some config to accept requests from any origin?
c.Writer.Header().Set("Access-Control-Allow-Origin", "*")
Thanks!