package utils import ( "encoding/json" "fmt" "io/ioutil" "net/http" "strings" ) // PostEdbLibRequest 调用指标接口 func PostEdbLibRequest(param map[string]interface{}, method string) (result []byte, err error) { postUrl := EDB_LIB_URL + method postData, err := json.Marshal(param) if err != nil { return } result, err = HttpPostRequest(postUrl, string(postData), "application/json") if err != nil { return } return } func HttpPostRequest(url, postData string, params ...string) ([]byte, error) { fmt.Println("HttpPost Url:" + url) body := ioutil.NopCloser(strings.NewReader(postData)) client := &http.Client{} req, err := http.NewRequest("POST", url, body) if err != nil { return nil, err } contentType := "application/x-www-form-urlencoded;charset=utf-8" if len(params) > 0 && params[0] != "" { contentType = params[0] } req.Header.Set("Content-Type", contentType) req.Header.Set("authorization", MD5(APP_EDB_LIB_NAME_EN+EDB_LIB_Md5_KEY)) resp, err := client.Do(req) if err != nil { fmt.Println("client.Do err:" + err.Error()) return nil, err } defer resp.Body.Close() b, err := ioutil.ReadAll(resp.Body) if err != nil { fmt.Println("HttpPost:" + string(b)) } return b, err } func HttpGetRequest(url string) ([]byte, error) { res, err := http.Get(url) if err != nil { return nil, err } defer res.Body.Close() return ioutil.ReadAll(res.Body) }