提到curl,第一时间就是想到guzzle
安装
go get -u github.com/idoubi/goz
封装示例
package common
import (
"github.com/idoubi/goz"
"log"
)
//get请求
func CurlGet(url string) goz.ResponseBody {
cli := goz.NewClient()
resp, err := cli.Get(url)
if err != nil {
log.Fatalln(err)
}
content, err := resp.GetBody()
if err != nil {
log.Fatalln(err)
}
return content
}
//post请求
func CurlPost(url string,Headers map[string]interface{},FormParams map[string]interface{}) goz.ResponseBody {
cli := goz.NewClient()
resp, err := cli.Post(url,goz.Options{
Headers: Headers,
FormParams: FormParams,
})
if err != nil {
log.Fatalln(err)
}
content, err := resp.GetBody()
if err != nil {
log.Fatalln(err)
}
return content
}
网友评论