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条)

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器