cors icon indicating copy to clipboard operation
cors copied to clipboard

OPTIONS Verb 404

Open mikepc opened this issue 8 years ago • 14 comments

Here is my main app code:

package main

import (
	"bitbucket.org/frobl-inc/padsd/configuration"
	"time"

	"gopkg.in/gin-contrib/cors.v1"
	"gopkg.in/gin-gonic/gin.v1"
)

var config = configuration.Current

//Log is the main logger
var Log = configuration.Log

func main() {
	router := gin.Default()
	// CORS for https://foo.com and https://github.com origins, allowing:
	// - PUT and PATCH methods
	// - Origin header
	// - Credentials share
	// - Preflight requests cached for 12 hours
	router.Use(cors.New(cors.Config{
		AllowOrigins:     []string{"https://foo.com"},
		AllowMethods:     []string{"PUT", "PATCH"},
		AllowHeaders:     []string{"Origin"},
		ExposeHeaders:    []string{"Content-Length"},
		AllowCredentials: true,
		AllowOriginFunc: func(origin string) bool {
			return origin == "https://github.com"
		},
		MaxAge: 12 * time.Hour,
	}))

	router.GET("/pads/healthcheck", healthCheck)

	router.Run()
}

When using a REST client (Postman) and executing the OPTIONS verb against /pads/healthcheck, the server returns a 404.

What am I doing wrong?

mikepc avatar Dec 08 '16 07:12 mikepc

You have defined only a route on get?

tboerger avatar Dec 08 '16 07:12 tboerger

If I have to define a route for OPTIONS, it really limits the usefulness of the library.

I wrote a middleware function to do the cors that seems to be working.

To me, when adding a CORS library to an api project I'm expecting that all routes will be covered by the library. Defining an OPTIONS route means I have do that for every single endpoint on the API which typically if I'm supplying a client-facing api, CORS will be required for all routes.

mikepc avatar Dec 08 '16 07:12 mikepc

You are right, cors is listening for options requests, but maybe you are running into https://github.com/gin-contrib/cors/blob/master/config.go#L33

tboerger avatar Dec 08 '16 07:12 tboerger

Yep that is EXACTLY what was wrong. My feedback here would be:

Since disallowed origins are returned with a 403, I would suggest if Origin is not present to return a 403 by default, and if AllowAllOrigins is enabled, Ignore the Origin header altogether (since a null/undefined origin would be assumed in the "All Origins")

mikepc avatar Dec 08 '16 07:12 mikepc

Thank you though, that was precisely what was wrong

mikepc avatar Dec 08 '16 07:12 mikepc

Also confused by this issue. When will the server return a 404?

ShuyangCao avatar May 27 '17 12:05 ShuyangCao

+1

cnBruceHong avatar Apr 23 '18 06:04 cnBruceHong

how ?

LeJane avatar May 14 '18 09:05 LeJane

Guys, I'm really lost!!! How to resolve this issue? Thanks

vzool avatar May 21 '19 12:05 vzool

package main

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

func main() {
	router := gin.Default()
	// same as
	// config := cors.DefaultConfig()
	// config.AllowAllOrigins = true
	// router.Use(cors.New(config))
	router.Use(cors.Default())
	router.GET("/ping", func(c *gin.Context) {
		c.JSON(200, gin.H{
			"message": "pong",
		})
	})
	router.Run("0.0.0.0:3000")
}

I receive a 404 with an OPTIONS request as well.

caiges avatar Dec 10 '19 16:12 caiges

I had a typo in the header Orgin: http://localhost instead of Origin: http://localhost I was sending, sorry for the noise.

caiges avatar Dec 10 '19 17:12 caiges

You are right, cors is listening for options requests, but maybe you are running into https://github.com/gin-contrib/cors/blob/master/config.go#L33

what does this mean?

ipg0 avatar Dec 16 '22 04:12 ipg0

Hi Folks,

This appears to still be open, and since I am having the same issue, are there any suggestions to resolving besides writing my own CORS handler (or, I guess, forking and fixing)?

Thanks.

cajund avatar Jan 09 '24 23:01 cajund

Hi Folks,

This appears to still be open, and since I am having the same issue, are there any suggestions to resolving besides writing my own CORS handler (or, I guess, forking and fixing)?

Thanks.

Found this middleware func from stackoverflow:

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, OPTIONS, GET, PUT")
        if c.Request.Method == "OPTIONS" {
            c.AbortWithStatus(204)
            return
        }
        c.Next()
    }
}

refuse2speak avatar Jan 11 '24 22:01 refuse2speak