V
V
Vadim Rublev2020-05-27 22:10:01
go
Vadim Rublev, 2020-05-27 22:10:01

Why does the Go server read cookies every other time (double)?

I'll ask again...

var ck http.Cookie

func main() {
    http.Handle("/", http.FileServer(http.Dir(Dir)))

    // В роуте читаем куки
    http.HandleFunc("/contacts", func(w http.ResponseWriter, r *http.Request) {
        _, err := r.Cookie("sessID")
        if err != nil {
            log.Println("Не считывается куки _sessID_ (по маршруту /contacts)")
        } else {
            var ck_ValueCont = ck.Value	        
            log.Println("Value куки _sessID_ по маршруту /contacts:", ck_ValueCont)
        }
        return
    })

    // В роуте авторизации создаём куки
    http.HandleFunc("/login", func(w http.ResponseWriter, r *http.Request) {
        ck = http.Cookie {
            Name: "sessID",
            Value: "token",
            Path: "localhost",
        }
        http.SetCookie(w, &ck)
        // Тут же читаем его - тест-индикатор о том, что куки создан.
        _, err := r.Cookie("sessID")
        if err != nil {
            log.Println("Не считывается куки _sessID_ (по маршруту /login)")
        } else {
            ck_ValueLogin = ck.Value
            log.Println("Value куки _sessID_ по маршруту /login:", ck_ValueLogin)
        }
        return
    })

log.Fatal(serv.ListenAndServeTLS(TLScert, TLSkey))
}

Why sometimes everything is fine - it is written and read; sometimes written (displayed by the test), but not read (by the /contacts route); sometimes it is not even recorded (test - error); and different browsers differently? Refresh pages with Ctrl+F5.

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