V
V
Vadim Rublev2020-05-23 16:42:42
go
Vadim Rublev, 2020-05-23 16:42:42

Why cookies are read through time?

Declared cookies in a global variable.
Further in one of the routes I send it to the client; and then I read it.
And in another route, I just read.
(Handling of -err errors is omitted here.)

var ck = http.Cookie {
        Name: "sessID",
        Value: "token_Sess",
}

func main() {
    http.HandleFunc("/login", func(w http.ResponseWriter, r *http.Request) {
        http.SetCookie(w, &ck)

        ck_Read, err := r.Cookie("sessID")
        ck_Value := ck_Read.Value
        log.Println("Значение куки _sessID_ по маршруту /login:", ck_Value)
        return
    })

    http.HandleFunc("/contacts", func(w http.ResponseWriter, r *http.Request) {
        coo, err := r.Cookie("sessID")
        coo_Value := coo.Value
        log.Println("Значение куки _sessID_ по маршруту /contacts:", coo_Value)
        return
    })
}

Why is the cookie read in the first route, but not in the second one (the error handler reports that there are no cookies)?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Andrey Tsvetkov, 2020-05-23
@yellow79

var ck = http.Cookie {
        Name: "sessID",
        Value: "token_Sess",
        Path: "/",
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question