mjHttpClient.go 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  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. func MojiWeatherPost(api string, data map[string]string) (result []byte, err error) {
  13. defer func() {
  14. l.Info("thirdparty",
  15. zap.String("api", api),
  16. zap.String("request", utils.MarshalJsonString(data)),
  17. zap.String("response", string(result)))
  18. }()
  19. values := url.Values{}
  20. values.Set("cityId", data["cityId"])
  21. //values.Set("token", data["token"])
  22. req, err := http.NewRequest("POST", api, bytes.NewBufferString(values.Encode()))
  23. req.Header.Add("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8")
  24. req.Header.Add("Authorization", "APPCODE "+config.Conf.ThirdPart.MojiAppCode)
  25. client := &http.Client{}
  26. client.Timeout = 30 * time.Second
  27. resp, err := client.Do(req)
  28. if err != nil {
  29. return nil, err
  30. }
  31. defer resp.Body.Close()
  32. result, err = ioutil.ReadAll(resp.Body)
  33. if err != nil {
  34. return nil, err
  35. }
  36. return result, nil
  37. }