config CORS in Gorilla Mux

712 views Asked by At

I am working in a golang project, using gorilla/mux as the router, and now I have problems related with CORS: I cannot make a POST request using ajax in another application.

My current configuration for the router is as follows:

r := mux.NewRouter()
    r.HandleFunc("/", handleHome)
    //Other routes

    headersOk := handlers.AllowedHeaders([]string{"*"})
    originsOk := handlers.AllowedOrigins([]string{"*"})
    methodsOk := handlers.AllowedMethods([]string{"GET", "HEAD", "POST", "PUT", "OPTIONS"})

    // Start http server
    port := fmt.Sprintf(":%d", some_Port)
    http.ListenAndServe(port, handlers.CORS(originsOk, headersOk, methodsOk)(r))

I am using:

"github.com/gorilla/mux"
"github.com/gorilla/handlers"

The message that I am getting in the browser is (in Spanish):

Solicitud desde otro origen bloqueada: la política de mismo origen impide leer el recurso remoto en https://miURL (razón: falta la cabecera CORS 'Access-Control-Allow-Origin').

In English: basically the server is rejecting the request because the CORS header is not present.

So, what have I done wrong in my router configuration?

EDIT:

The request sent

0

There are 0 answers