N
N
nano_e_t_42022-04-07 18:22:52
go
nano_e_t_4, 2022-04-07 18:22:52

How to set cookies in Go?

I'm trying to set Cookie in Golang, but for some reason it doesn't set ((
Tell me what I'm doing wrong.

func HandleUser(next http.Handler) http.Handler {
  return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
    if cookie, err := r.Cookie("ssid"); err == nil {
      logger("cookie is ", cookie)
    } else {
      logger(err.Error())
      expiration := time.Now().Add(365 * 24 * time.Hour)
      cookie := &http.Cookie{
        Name:     "ssid",
        Value:    "shop",
        Path:     "/",
        Expires:  expiration,
        HttpOnly: true,
        SameSite: 4,
        Secure:   true}
      http.SetCookie(w, cookie)
    }
    if cookie, err := r.Cookie("ssid"); err == nil {
      fmt.Println("cookie is ", cookie)
    }
    next.ServeHTTP(w, r)
  })
}

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question