cors icon indicating copy to clipboard operation
cors copied to clipboard

CORS request not applying correct on backend

Open mjubil1 opened this issue 5 years ago • 5 comments

Using the package and can not figure out why corsConfig is not applying to my localhost frontend. Any help will be much appreciated.

func main() {
	router := gin.Default()

	corsConfig := cors.New(cors.Options{
		AllowedOrigins: []string{"*"},
		AllowedMethods:     []string{"PUT", "PATCH", "GET", "POST", "DELETE", "OPTIONS"},
		AllowedHeaders: []string{"*"},
		AllowCredentials: false,
		MaxAge: int(12 * time.Hour),
	})
	
	router.Use(corsConfig)

	connDB, err := postgres.InitDB()     
	if err != nil {
		log.Fatal(err)
		return
	}

	defer connDB.Close()
	// Our server will live in the routes package
	routes.Run()
}

import (
	"github.com/gin-gonic/gin"
)

var (
	router = gin.Default()
)

// Run will start the server
func Run() {

	getRoutes()
	router.Run(":8000")
}

// getRoutes will create our routes of our entire application
// this way every group of routes can be defined in their own file
// so this one won't be so messy
func getRoutes() {
	v1 := router.Group("/v1")
	addUserRoutes(v1)
	addPingRoutes(v1)

	v2 := router.Group("/v2")
	addPingRoutes(v2)

	router.GET("/testing", func(c *gin.Context) {
		c.JSON(200, "hello")
	})
}
![Screenshot (2)](https://user-images.githubusercontent.com/16749587/93007572-325ab300-f538-11ea-9361-8f272c8e4ea7.png)


mjubil1 avatar Sep 13 '20 00:09 mjubil1

Image didn't post correctly. Screenshot (2)

mjubil1 avatar Sep 13 '20 00:09 mjubil1

it seems working for me

➜ curl -D - -H "Origin: google.com" http://localhost:8080/v1/
HTTP/1.1 200 OK
Access-Control-Allow-Origin: *
Content-Type: application/json; charset=utf-8
Vary: Origin
Date: Fri, 09 Oct 2020 02:05:13 GMT
Content-Length: 7

"hello"

➜ curl -D - -H "Origin: google.com" http://localhost:8080/v2/
HTTP/1.1 200 OK
Content-Type: application/json; charset=utf-8
Date: Fri, 09 Oct 2020 02:05:15 GMT
Content-Length: 10

"hello v2"

here is the code

package main

import (
	cors "github.com/rs/cors/wrapper/gin"
	"net/http"
	"time"

	"github.com/gin-gonic/gin"
)

func main() {
	router := gin.Default()

	corsConfig := cors.New(cors.Options{
		AllowedOrigins:   []string{"*"},
		AllowedMethods:   []string{"PUT", "PATCH", "GET", "POST", "DELETE", "OPTIONS"},
		AllowCredentials: false,
		MaxAge:           int(12 * time.Hour),
	})
	group := router.Group("/v1")
	group.Use(corsConfig)

	group.GET("/", func(c *gin.Context) {
		c.JSON(http.StatusOK, "hello")
	})
	group2 := router.Group("/v2")
	group2.GET("/", func(context *gin.Context) {
		context.JSON(http.StatusOK, "hello v2")
	})

	router.Run()
}

go.mod


go 1.13

require (
	github.com/gin-gonic/gin v1.6.3
	github.com/rs/cors v1.7.0
)

felixvo avatar Oct 09 '20 02:10 felixvo

Are you using Firefox? There seems to be an issue with pre-flight requests on Firefox.

conjohnerton avatar Nov 18 '20 16:11 conjohnerton

I was using Google Chrome I found a work around. It was a problem with calling the method inside my Run() function.

mjubil1 avatar Nov 25 '20 20:11 mjubil1

for anyone else coming here to look for an answer - I would honestly suggest just using a different language. Java/Javascript have much better designed libraries/frameworks for handling CORS easily.

akuzni2 avatar Nov 19 '21 02:11 akuzni2

@rs This one isn't reproducible and could likely be closed.

jub0bs avatar Sep 03 '23 19:09 jub0bs