request.go 2.3 KB

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