I am trying to send two PostForm requests to a specified URL(webpage), which one of them is "login" and another one is "create a product". The problem is the second request(create a product) runs apart from the first request(login). That is why although the login runs successfully, the server sends me a permission error("You must be logged in first to create a product.")
I thought, it's because of concurrency, thus I used channels and even time package as well, but none of them had worked. Is there any solution to handle it?
func login(c chan string) {
resp, _ := http.PostForm("http://example.com/login",
url.Values{"username": {"sth"}, "password": {"sth"}})
defer resp.Body.Close()
body, _ := ioutil.ReadAll(resp.Body)
time.Sleep(5000 * time.Millisecond)
fmt.Printf("%v", string(body), "
")
c <- string(body)
}
func CreateProduct() {
resp, _ := http.PostForm("http://example.com/product",
url.Values{"name": {"sth"}})
defer resp.Body.Close()
body, _ := ioutil.ReadAll(resp.Body)
fmt.Printf("%v", string(body), "
")
}
func main() {
c := make(chan string)
go login(c)
str := <-c
if str != "" {
CreateProduct()
}
}