0

I am trying to do a post request from react with axios to my golang microservice and I am getting an error

Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://127.0.0.1:4040/register. (Reason: CORS header ‘Access-Control-Allow-Origin’ missing).

This is my axios request

const req = {
            data: [
                {
                    type : "register",
                    attributes : {
                        username : "Some name",
                        password : "asdasdasdasdasd3",
                        name : "some name",
                        email : "somename@yahoo.com"
                    }
                }
            ]
        }
axios.post(`http://127.0.0.1:4040/register`, { req })
            .then(res => {
                console.log(res);
                console.log(res.data);
        })

and here is my endpoint in golang

    func register (w http.ResponseWriter, r *http.Request) {
            w.Header().Set("Access-Control-Allow-Origin", "*")
            w.Header().Set("Access-Control-Allow-Credentials", "true")
            w.Header().Set("Content-Type", "application/json");
            jsonBody := registerController.Perform(r)
            w.Write(jsonBody)
    }

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

        fmt.Println("server running at port " + SERVER_PORT)
        router.HandleFunc("/register", register).Methods("POST")

        http.ListenAndServe(SERVER_PORT, router)
}

I think I am missing something - could somebody help me with that

1 Answers1

-1

Your request header seems to be missing CSRF parameter.

Read up on it at the MDN page, to me it seems like you need to pass it in the request you're sending to your microservice.

https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS/Errors/CORSMissingAllowOrigin