当前位置: 首页 > 工具软件 > Go Curl > 使用案例 >

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

宦烈
2023-12-01

github源码网站:https://github.com/mikemintang/go-curl

如何安装包

go get github.com/mikemintang/go-curl

发送post请求:

package main

import (
    "fmt"
    "github.com/mikemintang/go-curl"
)

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.
    SetUrl(url).
        SetHeaders(headers).
        SetCookies(cookies).
        SetQueries(queries).
        SetPostData(postData).
        Post()

    if err != nil {
        fmt.Println(err)
    } else {
        if resp.IsOk() {
            fmt.Println(resp.Body)
        } else {
            fmt.Println(resp.Raw)
        }
    }

}
 类似资料: