util.go 898 B

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. package opay
  2. import (
  3. "io"
  4. "net/http"
  5. "strings"
  6. "bet24.com/log"
  7. )
  8. const dateFormat = "2006-01-02T15:04:05.000Z07:00"
  9. // http post json 请求
  10. func httpPostByJson(url string, data string, headers map[string]string) string {
  11. req, err := http.NewRequest("POST", url, strings.NewReader(data))
  12. if err != nil {
  13. log.Error("HttpPostByJson NewRequest error %v", err)
  14. return ""
  15. }
  16. req.Header.Add("Content-Type", "application/json")
  17. req.Header.Add("Cache-Control", "no-cache")
  18. for key, value := range headers {
  19. req.Header.Set(key, value)
  20. }
  21. resp, err := (&http.Client{}).Do(req)
  22. if err != nil {
  23. log.Error("HttpPostByJson Request Do error %v", err)
  24. return ""
  25. }
  26. defer resp.Body.Close()
  27. body, err := io.ReadAll(resp.Body)
  28. if err != nil {
  29. log.Error("HttpPostByJson Response error %v", err)
  30. return ""
  31. }
  32. // log.Debug("HttpPost Send:%v", string(body))
  33. return string(body)
  34. }