I am using mux package and have this code:
func saveConfig(w http.ResponseWriter, r *http.Request) {
if origin := r.Header.Get("Origin"); origin != "" {
w.Header().Set("Access-Control-Allow-Origin", origin)
fmt.Println("Origin: " + origin)
w.Header().Set("Access-Control-Allow-Methods", "POST, GET, OPTIONS, PUT, DELETE")
w.Header().Set("Access-Control-Allow-Headers", "Accept, Content-Type, Content-Length, Accept-Encoding, X-CSRF-Token, Authorization")
}
// Stop here if its Preflighted OPTIONS request
if r.Method == "OPTIONS" {
return
}
body, err := ioutil.ReadAll(io.LimitReader(r.Body, 1048576))
if err != nil {
fmt.Println("Error: %s
", err)
w.WriteHeader(http.StatusInternalServerError)
return
}
fmt.Println("JSON body:" + string(body))
if err := r.Body.Close(); err != nil {
panic(err)
}
w.WriteHeader(http.StatusCreated)
}
It's working fine on IE but chrome preflight is sending an OPTIONS method and I am getting 404 response back. Any help would be greatly appreciated.