msHttpClient.go 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. package thirdparty
  2. import (
  3. "bytes"
  4. "gd_management/common.in/config"
  5. "gd_management/common.in/utils"
  6. "go.uber.org/zap"
  7. "io/ioutil"
  8. "net/http"
  9. "net/url"
  10. "time"
  11. )
  12. /*const (
  13. TOKEN = "81ed9993bcad8cd0d1f55f9565104b76"
  14. )
  15. */
  16. func msConcatPostURL(api string) string {
  17. return api + "?token=" + config.Conf.ThirdPart.MsToken
  18. }
  19. func MsHttpClient(api string, values url.Values) (result []byte, err error) {
  20. client := &http.Client{}
  21. client.Timeout = 30 * time.Second
  22. defer func() {
  23. l.Info("thirdparty",
  24. zap.String("api", api),
  25. zap.String("request", utils.MarshalJsonString(values)),
  26. zap.String("response", utils.MarshalJsonString(result)))
  27. }()
  28. req, err := http.NewRequest("POST", msConcatPostURL(api), bytes.NewBufferString(values.Encode()))
  29. if err != nil {
  30. return nil, err
  31. }
  32. req.Header.Add("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8")
  33. resp, err := client.Do(req)
  34. if err != nil {
  35. return nil, err
  36. }
  37. defer resp.Body.Close()
  38. result, err = ioutil.ReadAll(resp.Body)
  39. if err != nil {
  40. return nil, err
  41. }
  42. return result, nil
  43. }