Cross-Origin Golang with gorilla / mux

0

In my application I am using the gorilla/mux package next to gorilla/handlers to enable / configure CORS of my application, I currently have an api in GO and a frontend application in vue , in my application vue that is on the 8080 port I try to call api on GO that stays on port 8081 and even with all CORS configured I keep getting error Cross-origin

func main() {
    router := mux.NewRouter()

    allowedHeaders := handlers.AllowedHeaders([]string{"X-Requested-With"})
    allowedOrigins := handlers.AllowedOrigins([]string{"*"})
    allowedMethods := handlers.AllowedMethods([]string{"GET", "HEAD", "POST", "PUT", "DELETE", "OPTIONS"})

    router.HandleFunc("/api/get/{name}", getCard)
    router.HandleFunc("/api/autocomplete/{name}", autoComplete)

    http.ListenAndServe(":8081", handlers.CORS(allowedHeaders, allowedOrigins, allowedMethods)(router))
}

If I access the endpoint localhost:8081/api/autocomplete/random from the browser or even from the terminal with curl the information is displayed normally, if I try to make a GET from my front-end application on another port I get the error below;

  

Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at link . (Reason: CORS header 'Access-Control-Allow-Origin' missing).

What else do I need to configure besides these handlers that I'm already using?

    
asked by anonymous 31.05.2018 / 17:25

1 answer

1

Just add the Content-Type as Header, the allowedHeaders variable will look like this:

allowedHeaders := handlers.AllowedHeaders([]string{"X-Requested-With", "Content-Type"})
    
20.06.2018 / 19:06