Can i get the session-ID which get saved into the cookies after session has been saved?

I am using gorilla/session and I need the session generated token(ID) which is going to be saved in cookies, as i want to save that token in database too. If I print the http response i.e fmt.Println(w), I’m able to see the session ID as:

&{0xc420204000 0xc42012e300 0xc420162840 0x118a4b0 false false false false 0xc420162280 {0xc4201a6460 map[] false false} map[Set-Cookie:[session=MTUyNzQ5MzA3M3xEdi1CQkFFQ180SUFBUkFCRUFBQUpmLUNBQUVHYzNSeWFXNW5EQW9BQ0hWelpYSnVZVzFsQm5OMGNtbHVad3dGQUFOaGMyUT18l1yx3yhYtbpiBRIagYseK80RSRC6warI07QxvmbuaDA=; Path=/; Expires=Wed, 27 Jun 2018 07:37:53 GMT; Max-Age=2592000]] true 0 -1 0 false false [] 0 [0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0] [0 0 0 0 0 0 0 0 0 0] [0 0 0] 0xc4202140e0 0}

how to store this session ID in a variable, so that i can use it later.

func MyHandler(w http.ResponseWriter, r *http.Request) {
		// Get a session. We're ignoring the error resulted from decoding an
		// existing session: Get() always returns a session, even if empty.
		session, _ := store.Get(r, "session-name")
		// Set some session values.
		session.Values["foo"] = "bar"
		session.Values[42] = 43
		// Save it before we write to the response/return from the handler.
		session.Save(r, w)
	}

The documentations shows us that sessions.Session is

type Session struct {
    // The ID of the session, generated by stores. It should not be used for
    // user data.
    ID  string
    // Values contains the user-data for the session.
    Values  map[interface{}]interface{}
    Options *Options
    IsNew   bool
    // contains filtered or unexported fields
}

session.ID should give you the session ID.

1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.