go-chi / cors

CORS net/http middleware for Go
MIT License
332 stars 32 forks source link

CORS isn't work #8

Closed grtfou closed 5 years ago

grtfou commented 5 years ago

I try the sample code

func main() {
    r := chi.NewRouter()

    // Basic CORS
    cors := cors.New(cors.Options{
        AllowedOrigins:   []string{"https://foo.com"},
        AllowedMethods:   []string{"POST", "OPTIONS"},
        AllowedHeaders:   []string{"Accept", "Authorization", "Content-Type", "X-CSRF-Token"},
        ExposedHeaders:   []string{"Link"},
        AllowCredentials: true,
        MaxAge:           300,
    })
    r.Use(cors.Handler)

    r.Get("/", func(w http.ResponseWriter, r *http.Request) {
        w.Write([]byte("welcome"))
    })

    http.ListenAndServe("127.0.0.1:3000", r)
}
$ curl -H "Origin: http://example.com" http://localhost:3000/
welcome     # ?

Doesn't any setting I lost?

Thank you. :)

VojtechVitek commented 5 years ago

https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS#Simple_requests