http.go 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. package utils
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "fmt"
  6. "io"
  7. "net/http"
  8. )
  9. func CheckZipExist(url string) (bool, error) {
  10. res, err := http.Get(url)
  11. if err != nil {
  12. return false, err
  13. }
  14. defer res.Body.Close()
  15. buf := make([]byte, 1024)
  16. n, err := res.Body.Read(buf)
  17. if err != nil {
  18. return false, err
  19. }
  20. if n != 1024 {
  21. return true, nil
  22. }
  23. return false, nil
  24. }
  25. // 返回succ,fail
  26. func PostJson(data []byte) (string, error) {
  27. req, err := http.NewRequest("POST", VersionApi, bytes.NewBuffer(data))
  28. if err != nil {
  29. return "fail", err
  30. }
  31. req.Header.Set("Content-Type", "application/json")
  32. client := &http.Client{}
  33. resp, err := client.Do(req)
  34. if err != nil {
  35. return "fail", err
  36. }
  37. defer resp.Body.Close()
  38. body, err := io.ReadAll(resp.Body)
  39. if err != nil {
  40. return "fail", err
  41. }
  42. return string(body), nil
  43. }
  44. func GetAppVersion(query string) (*AppVersion, error) {
  45. url := fmt.Sprintf("%s%s", VersionApi, query)
  46. client := &http.Client{}
  47. resp, err := client.Get(url)
  48. if err != nil {
  49. return nil, err
  50. }
  51. defer resp.Body.Close()
  52. body, err := io.ReadAll(resp.Body)
  53. if err != nil {
  54. return nil, err
  55. }
  56. appVersion := &AppVersion{}
  57. err = json.Unmarshal(body, appVersion)
  58. if err != nil {
  59. return nil, err
  60. }
  61. return appVersion, nil
  62. }