-
Notifications
You must be signed in to change notification settings - Fork 1.9k
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Add Access-Control-Allowed-Methods with mux.Route.Methods() #353
Comments
You could implement this as middleware by calling e.g. func AutoCORS(next http.Handler) http.Handler {
fn := func(w http.ResponseWriter, r *http.Request) {
route := mux.CurrentRoute(r)
methods, err := route.GetMethods()
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Header().Set("Access-Control-Allow-Methods", strings.Join(methods,
","))
next.ServeHTTP(w, r)
}
return http.HandlerFunc(fn)
} |
@elithrar Thank you for the response! That somewhat works I think, but OPTIONS requests still don't work right. Because the two URL's are the same format, an OPTIONS request to /reports/{eventKey}/{matchKey} or /reports/{eventKey}/{team} returns either PUT, OPTIONS or GET, OPTIONS (assuming both .Methods() include OPTIONS) when they should return GET, PUT, OPTIONS. Is there any way around this? To accumulate all methods on "the same" format URL's? |
bump |
Thinking on this more:
I'd be open to a PR that can do this as a feature on |
@elithrar I would be into trying to get that working myself. |
That'd be great @fharding1 - if you can get a PR up, I'll do my best to be more responsive to review! |
What version of Go are you running? (Paste the output of
go version
)go version go1.9.3 linux/amd64
What version of gorilla/mux are you at? (Paste the output of
git rev-parse HEAD
inside$GOPATH/src/github.com/gorilla/mux
)c0091a0
Describe your problem (and what you have tried so far)
When using mux.Route.Methods() to set allowed methods, there is no easy way to add CORS. I had a middleware for this, but you run into problems when you have two of "the same" URLs, for example
/reports/{eventKey}/{matchKey}
(PUT) and/reports/{eventKey}/{team}
(GET). It would be nice if there was a simple way to integrate CORS with .Methods(), or if there is one already if it were more clear in the documentation.An OPTIONS request or request to either endpoint should have Access-Control-Allow-Methods: "GET", "PUT", "OPTIONS".
Paste a minimal, runnable, reproduction of your issue below (use backticks to format it)
N/A
The text was updated successfully, but these errors were encountered: