I'm tryin to repeat a few http requests, that were performed using cURL, with Go.
In first request I'm getting cookie named "yandexuid":
$ curl --cookie-jar jar --output /dev/null \
https://oauth.yandex.ru/...&client_id=ID
Then I do POST with authentication data:
$ curl -v --cookie jar --cookie-jar jar \
--data 'login=LOGIN' \
--data 'passwd=PASS' \
--data 'twoweeks=no' \
--data 'retpath=https://oauth.yandex.ru/...type=token' \
https://passport.yandex.ru/auth?...&mode=qr
It returns many new cookies that will be used on the next step.
When I trying to do it with Go, first part works fine: cookie "yandexuid" comes and I attach it to next request. But second request don't returns cookies. Server respond's me with redirect page where it says: "Request processing error. An error has occurred. To log in to Yandex correctly, you need to enable cookies in your browser settings."
My Go code:
type Client struct {
HTTPClient *http.Client
CookieJar []http.Cookie
}
func (c* Client) AddCookies(cookies []*http.Cookie) {
for _, f := range cookies {
c.CookieJar = append(c.CookieJar, *f)
}
}
func (c *Client) Request(method string, request string, body io.Reader) *http.Response {
req, err := http.NewRequest(method, request, body)
for _, f := range c.CookieJar {
req.AddCookie(&f) //attach cookies saving in Client structure
}
req.Header.Set("X-Org-id", CompanyID)
req.Header.Set("Cache-Control", "no-cache")
resp, err := c.HTTPClient.Do(req)
return resp
}
func main() {
// first request
_url := fmt.Sprintf(https://oauth.yandex.ru/...&client_id=%s, ClientID)
resp := c.Request("GET", _url, nil)
c.AddCookies(resp.Cookies()) // getting cookies from respond and saving it in Client structure
// second request
PostData := strings.NewReader("login="+email+"&passwd="+pass+"&twoweeks=no&retpath=https://oauth.yandex.ru/...type=token")
resp = c.Request("POST", "https://passport.yandex.ru/auth?...&mode=qr", PostData)
}
What's difference between cURL and my Go requests? I know I do something wrong with cookies and as a result server think user's browser not saving cookies.