/
main.go
56 lines (47 loc) · 1.15 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package main
import (
"fmt"
"net/http"
"strconv"
"time"
)
func cookieSetter(w http.ResponseWriter, r *http.Request) {
counter, err := r.Cookie("counter")
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
value, err := strconv.Atoi(counter.Value)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
value = value + 1
newCookie := http.Cookie{
Name: "counter",
Value: strconv.Itoa(value),
}
http.SetCookie(w, &newCookie)
w.WriteHeader(http.StatusOK)
return
}
func main() {
http.HandleFunc("/cookie", cookieSetter)
go http.ListenAndServe(":8090", nil)
url := "http://:8090/cookie"
req, err := http.NewRequest("GET",url,nil)
if err != nil {
panic(err)
}
client := http.Client{}
c := http.Cookie{
Name:"counter", Value:"1", Domain: "127.0.0.1",
Path: "/", Expires: time.Now().AddDate(1,0,0)}
req.AddCookie(&c)
fmt.Println("-->", req.Header)
resp, err := client.Do(req)
if err != nil {
panic(err)
}
fmt.Println("<--",resp.Header)
}