Search code examples
httpgohttp-headerscorspreflight

Preflight issue with http POST in golang


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\n", 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.


Solution

  • The code registers for POST, but not OPTIONS. One approach is to change your code to the following:

    func NewRouter() *mux.Router { 
        router := mux.NewRouter().StrictSlash(true) 
        for _, route := range routes { 
            var handler http.Handler 
            handler = route.HandlerFunc
            handler = commonlibrary.Logger(handler, route.Name) 
            return router.Methods(route.Method, "OPTIONS").Path(route.Pattern).Name(route.Name).Handler(handler)
     }
    

    This will add OPTIONS to all handlers. Another approach is to change the Route Method field to Methods []string and create the router as:

    return router.Methods(route.Methods..., "OPTIONS").Path(route.Pattern).Name(route.Name).Handler(handler)
    

    This will allow you to add OPTIONS to a subset of the handlers.

    Yet another approach is to register a separate handler for OPTIONS:

     Route{"saveConfig", "OPTIONS", "/saveConfig", preflight}