I'm deploying a simple HTTP server and a simple HTTP client in Go. I tried to read the cookie value from the client without success (I receive an empty value) even if on the server the value that I see is set. How can I read the cookie value from the client side (i.e. to send the further requests by including the cookie that I received)?
Server Code
package main
import (
"log"
"math/rand"
"net/http"
"strconv"
"strings"
"time"
)
var SERVER_PORT string = ":8080"
func setCookie(w http.ResponseWriter, username string){
cookieValue := username + ":" + (username+strconv.Itoa(rand.Intn(100000000)))
//expiration := time.Now().Add(365 * 24 * time.Hour)
expiration := time.Now().Add(20 * time.Second)
cookie := http.Cookie{Name:"SessionID", Value: cookieValue, Expires: expiration}
http.SetCookie(w, &cookie)
log.Print(cookie)
}
func sayHello(w http.ResponseWriter, r* http.Request) {
username := r.URL.Path
username = strings.TrimPrefix(username, "/")
setCookie(w,username)
message := "Hello " + username
w.Write([]byte(message))
}
func ReadCookieServer(w http.ResponseWriter, req *http.Request) {
// read cookie
var cookie,err = req.Cookie("SessionID")
if err == nil {
var cookievalue = cookie.Value
w.Write([]byte(cookievalue))
}
}
func main() {
http.HandleFunc("/", sayHello)
http.HandleFunc("/readcookie", ReadCookieServer)
if err := http.ListenAndServe(SERVER_PORT, nil); err != nil {
panic(err)
}
}
Client Code
package main
import (
"io/ioutil"
"log"
http "net/http"
)
var serverName string = "http://localhost"
var serverPort string = ":8080/"
func MakeRequest() {
var username string = "blabla"
resp, err := http.Get(serverName + serverPort + username)
if err != nil {
log.Fatalln(err)
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Fatalln(err)
}
log.Println(string(body))
}
func Readcookie() {
resp, err := http.Get(serverName + serverPort + "readcookie")
if err != nil {
log.Fatalln(err)
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Fatalln(err)
}
log.Println(string(body))
}
func main() {
MakeRequest()
Readcookie()
}
If you want to read the cookie from an HTTP request, you do it with the Cookies
method. Example:
func Readcookie() {
resp, err := http.Get(serverName + serverPort + "readcookie")
if err != nil {
log.Fatalln(err)
}
for _, cookie := range resp.Cookies() {
if cookie.Name == "SessionID" {
log.Println("Cookie: ", cookie.Value)
}
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Fatalln(err)
}
log.Println(string(body))
}
To achieve your larger goal, of session management across subsequent HTTP requests, simply instantiate a cookie jar on your HTTP client. Example:
var client *http.Client
func init() {
jar, err := cookiejar.New(&cookiejar.Options{PublicSuffixList: publicsuffix.List})
if err != nil {
panic(err)
}
client = &http.Client{Jar: jar}
}
Then use this client (rather than the default) for your subsequent requests:
func Readcookie() {
resp, err := client.Get(serverName + serverPort + "readcookie")
if err != nil {
log.Fatalln(err)
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Fatalln(err)
}
log.Println(string(body))
}