http_request.go 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. package utils
  2. import (
  3. "encoding/json"
  4. "io/ioutil"
  5. "net/http"
  6. "strings"
  7. )
  8. // PostEdbLibRequest 调用指标接口
  9. func PostEdbLibRequest(param map[string]interface{}, method string) (result []byte, err error) {
  10. postUrl := EDB_LIB_URL + method
  11. postData, err := json.Marshal(param)
  12. if err != nil {
  13. return
  14. }
  15. result, err = HttpPostRequest(postUrl, string(postData), "application/json")
  16. if err != nil {
  17. return
  18. }
  19. return
  20. }
  21. func HttpPostRequest(url, postData string, params ...string) ([]byte, error) {
  22. body := ioutil.NopCloser(strings.NewReader(postData))
  23. client := &http.Client{}
  24. req, err := http.NewRequest("POST", url, body)
  25. if err != nil {
  26. return nil, err
  27. }
  28. contentType := "application/x-www-form-urlencoded;charset=utf-8"
  29. if len(params) > 0 && params[0] != "" {
  30. contentType = params[0]
  31. }
  32. req.Header.Set("Content-Type", contentType)
  33. req.Header.Set("authorization", MD5(APP_EDB_LIB_NAME_EN+EDB_LIB_Md5_KEY))
  34. resp, err := client.Do(req)
  35. if err != nil {
  36. FileLog.Error("client.Do err:" + err.Error())
  37. return nil, err
  38. }
  39. defer resp.Body.Close()
  40. b, err := ioutil.ReadAll(resp.Body)
  41. if err != nil {
  42. FileLog.Error("HttpPost:" + string(b))
  43. }
  44. return b, err
  45. }
  46. func HttpGetRequest(url string) ([]byte, error) {
  47. res, err := http.Get(url)
  48. if err != nil {
  49. return nil, err
  50. }
  51. defer res.Body.Close()
  52. return ioutil.ReadAll(res.Body)
  53. }