安装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)
}
}
}
智一面王老师说运维推荐初级Golang开发工程师在线评测:http://www.gtalent.cn/exam/interview/FM2N7xj54dQBPfUE
王老师说运维之go语言菜鸟入门:http://www.codeforest.cn/course/474