为了测试,我喜欢模拟注册。我得到注册页面,填写表格并发布。显然,服务器提供的会话cookie不会在post请求中发送。如果我从Web浏览器访问服务器一切正常。我可以看到对Get的响应包含cookie。如何将其添加到PostForm?
func signup(name string, ret chan bool) {
var xsrf string
fmt.Println("Starting signup with", name)
response, err := http.Get("http://localhost:8080/signup")
if err != nil {
panic(err)
} else {
defer response.Body.Close()
buffer, _ := ioutil.ReadAll(response.Body)
xsrf = regXsrf.FindStringSubmatch(string(buffer))[1]
}
data := url.Values{}
data.Set("name", name)
data.Add("password", "111222")
data.Add("password2", "111222")
data.Add("groupcode", "AllesWirdGut")
data.Add("websocketstoken", xsrf)
response, err = http.PostForm("http://localhost:8080/signup", data)
if err != nil {
panic(err)
} else {
defer response.Body.Close()
}
}