初始
This commit is contained in:
48
api/hwy/client/client.go
Normal file
48
api/hwy/client/client.go
Normal file
@ -0,0 +1,48 @@
|
||||
package client
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"github.com/gogf/gf/v2/frame/g"
|
||||
"github.com/gogf/gf/v2/os/gctx"
|
||||
"github.com/tiger1103/gfast/v3/api/hwy/sign"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"net/url"
|
||||
)
|
||||
|
||||
// 封装 Get 请求
|
||||
func Get(uri string, params map[string]string) (error, []byte) {
|
||||
joinPath, _ := url.JoinPath(sign.HOST, uri)
|
||||
url := joinPath + "?" + sign.CreateLinkString(params)
|
||||
response, err := g.Client().ContentJson().Get(gctx.New(), url)
|
||||
if err != nil {
|
||||
return err, []byte{}
|
||||
}
|
||||
bt := response.ReadAll()
|
||||
return nil, bt
|
||||
}
|
||||
|
||||
// 封装 Post 请求
|
||||
func Post(uri string, params map[string]string) (error, []byte) {
|
||||
// 把 map 转为 json
|
||||
jsonData, err := json.Marshal(params)
|
||||
if err != nil {
|
||||
return err, nil
|
||||
}
|
||||
|
||||
// 使用http包发送POST请求
|
||||
response, err := http.Post(sign.HOST+uri, "application/json", bytes.NewBuffer(jsonData))
|
||||
if err != nil {
|
||||
return err, nil
|
||||
}
|
||||
defer response.Body.Close()
|
||||
|
||||
// 读取响应体
|
||||
body, err := ioutil.ReadAll(response.Body)
|
||||
if err != nil {
|
||||
return err, nil
|
||||
}
|
||||
|
||||
return nil, body
|
||||
}
|
Reference in New Issue
Block a user