client.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. package mhayaHttp
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. clog "github.com/mhaya/logger"
  6. "io"
  7. "net/http"
  8. "net/url"
  9. "strings"
  10. "sync"
  11. "time"
  12. )
  13. var (
  14. postContentType = "application/x-www-form-urlencoded"
  15. jsonContentType = "application/json"
  16. DefaultTimeout = 5 * time.Second
  17. mu sync.Mutex
  18. )
  19. func GET(httpURL string, values ...map[string]string) ([]byte, *http.Response, error) {
  20. client := http.Client{Timeout: DefaultTimeout}
  21. if len(values) > 0 {
  22. rst := ToUrlValues(values[0])
  23. httpURL = AddParams(httpURL, rst)
  24. }
  25. rsp, err := client.Get(httpURL)
  26. if err != nil {
  27. return nil, rsp, err
  28. }
  29. defer func(body io.ReadCloser) {
  30. e := body.Close()
  31. if e != nil {
  32. clog.Warnf("HTTP GET [url = %s], error = %s", httpURL, e)
  33. }
  34. }(rsp.Body)
  35. bodyBytes, err := io.ReadAll(rsp.Body)
  36. if err != nil {
  37. return nil, rsp, err
  38. }
  39. return bodyBytes, rsp, nil
  40. }
  41. func POST(httpURL string, values map[string]string) ([]byte, *http.Response, error) {
  42. client := http.Client{Timeout: DefaultTimeout}
  43. rst := ToUrlValues(values)
  44. rsp, err := client.Post(httpURL, postContentType, strings.NewReader(rst.Encode()))
  45. if err != nil {
  46. return nil, rsp, err
  47. }
  48. defer func(body io.ReadCloser) {
  49. e := body.Close()
  50. if e != nil {
  51. clog.Warnf("HTTP POST [url = %s], error = %s", httpURL, e)
  52. }
  53. }(rsp.Body)
  54. bodyBytes, err := io.ReadAll(rsp.Body)
  55. if err != nil {
  56. return nil, rsp, err
  57. }
  58. return bodyBytes, rsp, nil
  59. }
  60. func PostJSON(httpURL string, values map[string]interface{}) ([]byte, *http.Response, error) {
  61. client := http.Client{Timeout: DefaultTimeout}
  62. jsonBytes, err := json.Marshal(values)
  63. if err != nil {
  64. return nil, nil, err
  65. }
  66. rsp, err := client.Post(httpURL, jsonContentType, bytes.NewBuffer(jsonBytes))
  67. if err != nil {
  68. return nil, rsp, err
  69. }
  70. defer func(body io.ReadCloser) {
  71. e := body.Close()
  72. if e != nil {
  73. clog.Warnf("HTTP PostJSON [url = %s], error = %s", httpURL, e)
  74. }
  75. }(rsp.Body)
  76. bodyBytes, err := io.ReadAll(rsp.Body)
  77. if err != nil {
  78. return nil, rsp, err
  79. }
  80. return bodyBytes, rsp, nil
  81. }
  82. func PostJSONWithHeader(httpURL string, values map[string]interface{}, token string) ([]byte, error) {
  83. client := &http.Client{}
  84. // 设置请求体,json格式
  85. bytesData, _ := json.Marshal(values)
  86. req, err := http.NewRequest("POST", httpURL,
  87. bytes.NewBuffer([]byte(bytesData)))
  88. if err != nil {
  89. clog.Warnf("HTTP PostJSONWithHeader [url = %s], error = %s", httpURL, err)
  90. return nil, err
  91. }
  92. //设置请求头
  93. req.Header.Set("content-type", jsonContentType)
  94. req.Header.Set("Token", token)
  95. resp, err := client.Do(req)
  96. if err != nil {
  97. clog.Debugf("HTTP PostJSONWithHeader [url = %s], error executing request: %v", httpURL, err)
  98. return nil, err
  99. }
  100. defer resp.Body.Close()
  101. content, err := io.ReadAll(resp.Body)
  102. if err != nil {
  103. clog.Warnf("HTTP PostJSONWithHeader [url = %s], error = %s", httpURL, err)
  104. return nil, err
  105. }
  106. return content, nil
  107. }
  108. func AddParams(httpURL string, params url.Values) string {
  109. if len(params) == 0 {
  110. return httpURL
  111. }
  112. if !strings.Contains(httpURL, "?") {
  113. httpURL += "?"
  114. }
  115. if strings.HasSuffix(httpURL, "?") || strings.HasSuffix(httpURL, "&") {
  116. httpURL += params.Encode()
  117. } else {
  118. httpURL += "&" + params.Encode()
  119. }
  120. return httpURL
  121. }
  122. func ToUrlValues(values map[string]string) url.Values {
  123. rst := make(url.Values)
  124. for k, v := range values {
  125. rst.Add(k, v)
  126. }
  127. return rst
  128. }