0

For testing I like to simulate signups. I get the signup page, fill in the form and post it. Apparently the session cookie that is provided by the server is not sent in the post request. If I access the server from a web browser all works fine. I can see that the response to Get contains the cookie. How can I add it to the 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()
    }   
}
Michael
  • 6,823
  • 11
  • 54
  • 84
  • 1
    You have to create a Cookiejar and send it along with the POST request. See this answer http://stackoverflow.com/a/12756986/1357978 – Nadh Jun 05 '16 at 09:29
  • You can use this one. response.Request.Cookie("cookie name") – Ario Jun 05 '16 at 09:33

0 Answers0