Hi Gophers,
I'm facing a very weird error:
func getUserName(w http.ResponseWriter, r *http.Request) string {
c, err := r.Cookie("session")
if err != nil {
http.Redirect(w, r, rootUrl, http.StatusSeeOther)
return ""
}
log.Println("session raw", c.Value)
var s session
fmt.Printf("%+v\n", s)
reader := strings.NewReader(c.Value)
if err := gob.NewDecoder(reader).Decode(&s); err != nil {
log.Println(err)
http.Error(w, "server error: gob decoding", http.StatusInternalServerError)
return ""
}
return s.Name // userName
}
With the above code, I'm getting the following:
2023/07/21 18:11:00 net/http: invalid byte '\x7f' in Cookie.Value; dropping invalid bytes
2023/07/21 18:11:00 session raw &sessionNameTextA
{Name: Text:}
2023/07/21 18:11:00 unexpected EOF
The Cookie session is encoded with gob Encode. It should only contain plain text strings right? So where does the problem come from?
The whole program is at:
The recent change that got me into the problem is:
Please help. Thx.