package wechat import ( "bytes" "encoding/json" "encoding/xml" "errors" "git.chuangxin1.com/cx/myth" ) func checkJSONError(msg myth.HTTPMessage) (jq *myth.JSONQuery, err error) { if msg.StatusCode == 200 { jq, err = msg.JSONQuery() if err != nil { return } code := int(0) code, err = jq.Int(`errcode`) if code == ErrReqOk { err = nil return } if err == nil { var msg string msg, err = jq.String("errmsg") if err != nil { return } err = errors.New(msg) } } else { err = errors.New(`HTTP StatusCode not 200`) } return } // getJSON 获取 JSON 格式返回数据 func getJSON(uri string) (jq *myth.JSONQuery, err error) { var msg myth.HTTPMessage headers := make(map[string]string) headers["Accept-Encoding"] = "gzip, deflate, br" headers["Accept"] = "application/json" if msg, err = myth.Get(uri, "", "", headers); err != nil { return } jq, err = checkJSONError(msg) return } // getBody 获取原始返回数据 func getBody(uri string) (msg myth.HTTPMessage, err error) { headers := make(map[string]string) headers["Accept-Encoding"] = "gzip, deflate, br" msg, err = myth.Get(uri, "", "", headers) if err != nil { return } if msg.StatusCode == 200 { jq := &myth.JSONQuery{} jq, err = msg.JSONQuery() if err != nil { return } code := int(0) code, err = jq.Int(`errcode`) if code == ErrReqOk { err = nil return } if err == nil { var errmsg string errmsg, err = jq.String("errmsg") if err != nil { return } err = errors.New(errmsg) } } else { err = errors.New(`HTTP StatusCode not 200`) } return } func postJSON(url string, args interface{}) (msg myth.HTTPMessage, err error) { headers := make(map[string]string) headers["Accept-Encoding"] = "gzip, deflate, br" headers["Content-Type"] = "application/json; charset=utf-8" var data []byte data, err = json.Marshal(args) if err != nil { return } msg, err = myth.Post(url, "", "", headers, bytes.NewReader(data)) return } func postJSONData(url string, data []byte) (msg myth.HTTPMessage, err error) { headers := make(map[string]string) headers["Accept-Encoding"] = "gzip, deflate, br" headers["Content-Type"] = "application/json; charset=utf-8" msg, err = myth.Post(url, "", "", headers, bytes.NewReader(data)) return } func postXML(url string, args interface{}) (msg myth.HTTPMessage, err error) { headers := make(map[string]string) headers["Accept-Encoding"] = "gzip, deflate, br" headers["Content-Type"] = "application/xml; charset=utf-8" var data []byte data, err = xml.Marshal(args) if err != nil { return } msg, err = myth.Post(url, "", "", headers, bytes.NewReader(data)) return }