Coder Social home page Coder Social logo

OPTIONS Verb 404 about cors HOT 14 OPEN

gin-contrib avatar gin-contrib commented on June 1, 2024 1
OPTIONS Verb 404

from cors.

Comments (14)

mikepc avatar mikepc commented on June 1, 2024 2

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.

from cors.

mikepc avatar mikepc commented on June 1, 2024 2

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")

from cors.

caiges avatar caiges commented on June 1, 2024 1

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

from cors.

tboerger avatar tboerger commented on June 1, 2024

You have defined only a route on get?

from cors.

tboerger avatar tboerger commented on June 1, 2024

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

from cors.

mikepc avatar mikepc commented on June 1, 2024

Thank you though, that was precisely what was wrong

from cors.

ShuyangCao avatar ShuyangCao commented on June 1, 2024

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

from cors.

cnBruceHong avatar cnBruceHong commented on June 1, 2024

+1

from cors.

LeJane avatar LeJane commented on June 1, 2024

how ?

from cors.

vzool avatar vzool commented on June 1, 2024

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

from cors.

caiges avatar caiges commented on June 1, 2024
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.

from cors.

ipg0 avatar ipg0 commented on June 1, 2024

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?

from cors.

cajund avatar cajund commented on June 1, 2024

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.

from cors.

refuse2speak avatar refuse2speak commented on June 1, 2024

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()
    }
}

from cors.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.