mv src/common src/utils src/grpc-etcdv3 to pkg

This commit is contained in:
xmcy0011
2021-10-11 22:12:01 +08:00
parent bc94d4e0b3
commit 737edb985b
119 changed files with 310 additions and 702 deletions
+53
View File
@@ -0,0 +1,53 @@
/*
** description("").
** copyright('open-im,www.open-im.io').
** author("fg,Gordon@tuoyun.net").
** time(2021/5/27 10:31).
*/
package http
import (
"bytes"
"encoding/json"
"io/ioutil"
"net/http"
"time"
)
func Get(url string) (response []byte, err error) {
client := http.Client{Timeout: 5 * time.Second}
resp, err := client.Get(url)
if err != nil {
return nil, err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
return body, nil
}
//application/json; charset=utf-8
func Post(url string, data interface{}, contentType string) (content []byte, err error) {
jsonStr, _ := json.Marshal(data)
req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonStr))
if err != nil {
return nil, err
}
req.Header.Add("content-type", contentType)
defer req.Body.Close()
client := &http.Client{Timeout: 5 * time.Second}
resp, err := client.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
result, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
return result, nil
}