Cors issue
bernardpel
PROOP

2 years ago

Hello how can call my endpoint from other domain?

I have golang app as my backend and I have a middleware to handle cors as shown below: But I still get 401 unauthorized status.

func CorsHandler(next http.Handler) http.Handler {

 return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {

logger.Info().Msg("got cors request")

w.Header().Set("Access-Control-Allow-Origin", "*") // TODO: need to change it to the domain from vercel frontend

w.Header().Add("Access-Control-Allow-Headers", "*")

w.Header().Add("Access-Control-Allow-Credentials", "true")

w.Header().Add("Access-Control-Allow-Methods", "POST, GET, OPTIONS, PUT, DELETE")

w.Header().Set("content-type", "application/json;charset=UTF-8")

if r.Method == "OPTIONS" {

w.WriteHeader(http.StatusOK)

logger.Info().Msg("responded with no content")

return

 }

next.ServeHTTP(w, r)

 })

}
Solved

3 Replies

brody
EMPLOYEE

2 years ago

Hey, you getting 401 unauthorized means cors isn't the issue here, if it was you would be getting a cors error with no status code.

Perhaps you forgot to add auth to the request?

Not entirely sure with the information given, please provide more information.


bernardpel
PROOP

2 years ago

Hi brody, you are right! I need to pass the apikey in the header. thanks man!


brody
EMPLOYEE

2 years ago

Awsome! am I good to mark as solved?


Status changed to Solved ray-chen over 1 year ago


Loading...