【go语言 curl】golang版本的curl请求库



go get github.com/mikemintang/go-curl


package main

import (

func main() {

    url := "http://php.dev/api.php"

    headers := map[string]string{
        "User-Agent":    "Sublime",
        "Authorization": "Bearer access_token",
        "Content-Type":  "application/json",

    cookies := map[string]string{
        "userId":    "12",
        "loginTime": "15045682199",

    queries := map[string]string{
        "page": "2",
        "act":  "update",

    postData := map[string]interface{}{
        "name":      "mike",
        "age":       24,
        "interests": []string{"basketball", "reading", "coding"},
        "isAdmin":   true,

    // 链式操作
    req := curl.NewRequest()
    resp, err := req.

    if err != nil {
    } else {
        if resp.IsOk() {
        } else {

已标记关键词 清除标记
<div class="post-text" itemprop="text"> <p>I'm tryin to repeat a few http requests, that were performed using cURL, with Go.</p> <p>In first request I'm getting cookie named "yandexuid":</p> <pre><code>$ curl --cookie-jar jar --output /dev/null \ https://oauth.yandex.ru/...&client_id=ID </code></pre> <p>Then I do POST with authentication data:</p> <pre><code>$ 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 </code></pre> <p>It returns many new cookies that will be used on the next step.</p> <p>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: "<i>Request processing error. An error has occurred. To log in to Yandex correctly, you need to enable cookies in your browser settings.</i>"</p> <p>My Go code:</p> <pre><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) } </code></pre> <p>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.</p> </div>
©️2020 CSDN 皮肤主题: 编程工作室 设计师:CSDN官方博客 返回首页