dongzhuner6981 2014-09-28 03:26
浏览 60
已采纳

获取现有的大猩猩会议

I am trying to set a gorilla session and then retreive the value again. I am doing the following just as a test.

//create session and store in http Cookies
session, err := store.Get(req, "session")
if err != nil {
    errCode = http.StatusInternalServerError
    return
}

//save a value
session.Values["user_id"] = userTuple.UserId
err = session.Save(req, w)
if err != nil {
    errCode = http.StatusInternalServerError
    return
}

//try to get the same session that was just created
nr := http.Request{Header: w.Header()}
session, err = store.Get(&nr, "session")
if err != nil {
    errCode = http.StatusInternalServerError
    return
} else if session.IsNew {
    log.Println("New session created instead of old one.")
}

This is a snippet out of a larger HTTP handler. But the relavent parts are posted and the the second call to store.Get() is not returning an existing session, but a brand new one. Hence, when the handler this code is in is executed, the log statements is printed to console.

Why am I getting a new session in this case instead of the one I had already created and saved?

  • 写回答

3条回答 默认 最新

  • du16178 2014-09-28 03:44
    关注

    I feel like I already answered this question. You are misusing this pkg. Behind the scenes the context package is used to store state for a request, by creating a new, incomplete http.Request, the context package returns an empty state with no session info.

    check out this code to see what i'm talking about.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?