0

I'm trying to send a GET request to gin-gonic backend server

const { data, headers } = await axios("http://192.168.178.2/users/@me", {
          method: "GET",
          withCredentials: true,
          headers: {
            Authorization: `Bearer ${token}`,
          },
        })

but some how the server cannot read the Authorization header. I tried to play with Origin header and stuff, but still doesn't work

func CorsMiddleware() gin.HandlerFunc {
    return func(c *gin.Context) {
        c.Writer.Header().Set("Access-Control-Allow-Origin", "*")
        c.Writer.Header().Set("Access-Control-Allow-Credentials", "true")
        c.Writer.Header().Set("Access-Control-Allow-Headers", "Content-Type, Content-Length, Accept-Encoding, X-CSRF-Token, Authorization, accept, origin, Cache-Control, X-Requested-With")
        c.Writer.Header().Set("Access-Control-Allow-Methods", "POST, OPTIONS, GET, PUT")

        if c.Request.Method == "OPTIONS" {
            c.AbortWithStatus(204)
            return
        }

        c.Next()
    }
}

And this is how I get the header data

func JwtMiddleware() gin.HandlerFunc {
    return func(c *gin.Context) {
        authHeader := c.Request.Header.Get("Authorization")
        if authHeader == "" {
            errorHandler.Unauthorized(c, http.StatusBadRequest, errorHandler.AuthorizationKeyNotFound)
            c.Abort()
            return
        }

        token, err := validateHeaders(c.GetHeader("Authorization"))
        if token == nil {
            errorHandler.Unauthorized(c, http.StatusBadRequest, *err)
            c.Abort()
            return
        }

When I log the c.GetHeader("Authorzation") out, it says ""

0 Answers0