Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

40 lines
973B

  1. package main
  2. import (
  3. "encoding/json"
  4. "net/http"
  5. "strconv"
  6. )
  7. //WechatError when API call goes wrong, it shows error message
  8. type WechatError struct {
  9. ErrCode int64 `json:"errcode"`
  10. ErrMsg string `json:"errmsg"`
  11. }
  12. //getWechatAPIError given an http body, try to decode json error
  13. // caller needs to makesure content type is json
  14. //return
  15. // the error strucutre, on success
  16. // err: if json decode encounters problem
  17. func getWechatAPIError(respBody string) (result WechatError, err error) {
  18. err = json.Unmarshal([]byte(respBody), &result)
  19. return
  20. }
  21. //see if a http response is json response
  22. func isJSON(r *http.Response) bool {
  23. contentType := r.Header.Get("Content-Type")
  24. contentLen := r.Header.Get("Content-Length")
  25. len, _ := strconv.Atoi(contentLen)
  26. if contentType == "text/plain" || contentType == "text/json" {
  27. if len < 10240 { //less than 10K
  28. return true
  29. }
  30. }
  31. return false
  32. }
  33. //ToDo: test error message @todo