1. 程式人生 > >【go語言 curl】golang版本的curl請求庫

【go語言 curl】golang版本的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) } } }