I hope you are doing well. Please, I have this RESTFul API build using Go and Gorilla/mux and rs/cors. Here is the router handler with cors set up
func NewRouter() *mux.Router {
r := mux.NewRouter().StrictSlash(true)
r.HandleFunc("/healthz", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode("I'm alive")
})
r.HandleFunc("/api/analysis/{projectId}", List).Methods(http.MethodGet, http.MethodOptions) // list all charts
r.HandleFunc("/api/analysis/{projectId}", Create).Methods(http.MethodPost, http.MethodOptions) // create a chart
r.HandleFunc("/api/analysis/{projectId}", Delete).Methods(http.MethodDelete, http.MethodOptions) // delete a chart
return r
}
Here is how I'm starting the server
r := NewRouter()
r.Use(loggingMiddleware)
handler := cors.Default().Handler(r)
log.Fatal(http.ListenAndServe(":5000", handler))
cors is from https://github.com/rs/cors
From an Angular Web application, I'm calling the route http://localhost:5000/api/analysis/6023d34440baf5016e5b74ea but I receive a Cors error: PreflightMissingAllowOriginHeader
From a React.js Web application, I don't receive any error, No errors !! Successful request
I have also tried the CORS configuration from MUX https://github.com/gorilla/mux#handling-cors-requests but it didn't also works.
Please, what am I doing wrong? How can I fix that?