request.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. //fmt.Println(msg.StatusCode, string(msg.Body))
  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. //fmt.Println(msg.StatusCode, string(msg.Body))
  56. if msg.StatusCode == 200 {
  57. jq := &myth.JSONQuery{}
  58. jq, err = msg.JSONQuery()
  59. if err != nil {
  60. return
  61. }
  62. code := int(0)
  63. code, err = jq.Int(`errcode`)
  64. if code == ErrReqOk {
  65. err = nil
  66. return
  67. }
  68. if err == nil {
  69. var errmsg string
  70. errmsg, err = jq.String("errmsg")
  71. if err != nil {
  72. return
  73. }
  74. err = errors.New(errmsg)
  75. }
  76. } else {
  77. err = errors.New(`HTTP StatusCode not 200`)
  78. }
  79. return
  80. }
  81. func postJSON(url string, args interface{}) (msg myth.HTTPMessage, err error) {
  82. headers := make(map[string]string)
  83. headers["Accept-Encoding"] = "gzip, deflate, br"
  84. headers["Content-Type"] = "application/json; charset=utf-8"
  85. var data []byte
  86. data, err = json.Marshal(args)
  87. if err != nil {
  88. return
  89. }
  90. msg, err = myth.Post(url, "", "", headers, bytes.NewReader(data))
  91. return
  92. }
  93. func postXML(url string, args interface{}) (msg myth.HTTPMessage, err error) {
  94. headers := make(map[string]string)
  95. headers["Accept-Encoding"] = "gzip, deflate, br"
  96. headers["Content-Type"] = "application/xml; charset=utf-8"
  97. var data []byte
  98. data, err = xml.Marshal(args)
  99. if err != nil {
  100. return
  101. }
  102. msg, err = myth.Post(url, "", "", headers, bytes.NewReader(data))
  103. return
  104. }