request.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. package wechat
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "encoding/xml"
  6. "errors"
  7. "fmt"
  8. "git.chuangxin1.com/cx/myth"
  9. )
  10. func checkJSONError(msg myth.HTTPMessage) (jq *myth.JSONQuery, err error) {
  11. if msg.StatusCode == 200 {
  12. jq, err = msg.JSONQuery()
  13. if err != nil {
  14. return
  15. }
  16. code := int(0)
  17. code, err = jq.Int(`errcode`)
  18. if code == ErrReqOk {
  19. err = nil
  20. return
  21. }
  22. if err == nil {
  23. var msg string
  24. msg, err = jq.String("errmsg")
  25. if err != nil {
  26. return
  27. }
  28. err = errors.New(msg)
  29. }
  30. } else {
  31. err = errors.New(`HTTP StatusCode not 200`)
  32. }
  33. return
  34. }
  35. // getJSON 获取 JSON 格式返回数据
  36. func getJSON(uri string) (jq *myth.JSONQuery, err error) {
  37. var msg myth.HTTPMessage
  38. headers := make(map[string]string)
  39. headers["Accept-Encoding"] = "gzip, deflate, br"
  40. headers["Accept"] = "application/json"
  41. if msg, err = myth.Get(uri, "", "", headers); err != nil {
  42. return
  43. }
  44. jq, err = checkJSONError(msg)
  45. return
  46. }
  47. // getBody 获取原始返回数据
  48. func getBody(uri string) (msg myth.HTTPMessage, err error) {
  49. headers := make(map[string]string)
  50. headers["Accept-Encoding"] = "gzip, deflate, br"
  51. msg, err = myth.Get(uri, "", "", headers)
  52. if err != nil {
  53. return
  54. }
  55. if msg.StatusCode == 200 {
  56. jq := &myth.JSONQuery{}
  57. jq, err = msg.JSONQuery()
  58. if err != nil {
  59. return
  60. }
  61. code := int(0)
  62. code, err = jq.Int(`errcode`)
  63. if code == ErrReqOk {
  64. err = nil
  65. return
  66. }
  67. if err == nil {
  68. var errmsg string
  69. errmsg, err = jq.String("errmsg")
  70. if err != nil {
  71. return
  72. }
  73. err = errors.New(errmsg)
  74. }
  75. } else {
  76. err = errors.New(`HTTP StatusCode not 200`)
  77. }
  78. return
  79. }
  80. func postJSON(url string, args interface{}) (msg myth.HTTPMessage, err error) {
  81. headers := make(map[string]string)
  82. headers["Accept-Encoding"] = "gzip, deflate, br"
  83. headers["Content-Type"] = "application/json; charset=utf-8"
  84. var data []byte
  85. data, err = json.Marshal(args)
  86. if err != nil {
  87. return
  88. }
  89. fmt.Println("postJSON", string(data))
  90. msg, err = myth.Post(url, "", "", headers, bytes.NewReader(data))
  91. return
  92. }
  93. func postJSONData(url string, data []byte) (msg myth.HTTPMessage, err error) {
  94. headers := make(map[string]string)
  95. headers["Accept-Encoding"] = "gzip, deflate, br"
  96. headers["Content-Type"] = "application/json; charset=utf-8"
  97. msg, err = myth.Post(url, "", "", headers, bytes.NewReader(data))
  98. return
  99. }
  100. func postXML(url string, args interface{}) (msg myth.HTTPMessage, err error) {
  101. headers := make(map[string]string)
  102. headers["Accept-Encoding"] = "gzip, deflate, br"
  103. headers["Content-Type"] = "application/xml; charset=utf-8"
  104. var data []byte
  105. data, err = xml.Marshal(args)
  106. if err != nil {
  107. return
  108. }
  109. msg, err = myth.Post(url, "", "", headers, bytes.NewReader(data))
  110. return
  111. }