util.go 823 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. package crush
  2. import (
  3. "io"
  4. "net/http"
  5. "strings"
  6. "bet24.com/log"
  7. )
  8. // http post json 请求
  9. func httpPostByJson(url string, data string, authorization string) string {
  10. req, err := http.NewRequest("POST", url, strings.NewReader(data))
  11. if err != nil {
  12. log.Error("HttpPostByJson NewRequest error %v", err)
  13. return ""
  14. }
  15. req.Header.Add("Content-Type", "application/json")
  16. req.Header.Add("Cache-Control", "no-cache")
  17. req.Header.Set("Authorization", authorization)
  18. resp, err := (&http.Client{}).Do(req)
  19. if err != nil {
  20. log.Error("HttpPostByJson Request Do error %v", err)
  21. return ""
  22. }
  23. defer resp.Body.Close()
  24. body, err := io.ReadAll(resp.Body)
  25. if err != nil {
  26. log.Error("HttpPostByJson Response error %v", err)
  27. return ""
  28. }
  29. // log.Debug("HttpPost Send:%v", string(body))
  30. return string(body)
  31. }