改进ghttp client request 和server response封装方法

This commit is contained in:
John
2017-12-26 15:41:10 +08:00
parent 165eccede6
commit ae858149bb
2 changed files with 9 additions and 13 deletions

View File

@ -161,20 +161,16 @@ func (r *ClientRequest) GetRequestMap(defaultMap map[string][]string) map[string
// 获取原始请求输入字符串
func (r *ClientRequest) GetRaw() string {
result, err := ioutil.ReadAll(r.Body)
if err != nil {
return ""
} else {
return string(result)
}
func (r *ClientRequest) GetRaw() []byte {
result, _ := ioutil.ReadAll(r.Body)
return result
}
// 获取原始请求输入字符串
func (r *ClientRequest) GetJson() *gjson.Json {
data := r.GetRaw()
if data != "" {
if j, err := gjson.DecodeToJson([]byte(data)); err == nil {
if data != nil {
if j, err := gjson.DecodeToJson(data); err == nil {
return j
}
}

View File

@ -15,9 +15,9 @@ type ServerResponse struct {
// 返回的固定JSON数据结构
type ResponseJson struct {
Result int `json:"result"`
Message string `json:"message"`
Data interface{} `json:"data"`
Result int `json:"result"` // 标识消息状态
Message string `json:"message"` // 消息使用string存储
Data []byte `json:"data"` // 二进制数据(不管什么数据结构)
}
// 返回信息(byte)
@ -35,7 +35,7 @@ func (r *ServerResponse) WriteString(content string) {
}
// 返回固定格式的json
func (r *ServerResponse) WriteJson(result int, message string, data interface{}) error {
func (r *ServerResponse) WriteJson(result int, message string, data []byte) error {
r.Header().Set("Content-Type", "application/json")
r.bufmu.Lock()
defer r.bufmu.Unlock()