doupa2871 2019-01-19 21:10 采纳率: 100%
浏览 99

cURL和我的Go请求之间有什么区别?

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.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 关于#python#的问题:求帮写python代码
    • ¥20 MATLAB画图图形出现上下震荡的线条
    • ¥15 LiBeAs的带隙等于0.997eV,计算阴离子的N和P
    • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
    • ¥15 来真人,不要ai!matlab有关常微分方程的问题求解决,
    • ¥15 perl MISA分析p3_in脚本出错
    • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
    • ¥15 ubuntu虚拟机打包apk错误
    • ¥199 rust编程架构设计的方案 有偿
    • ¥15 回答4f系统的像差计算