mirror of
https://gitee.com/johng/gf
synced 2026-06-07 02:12:11 +08:00
改进ghttp client request 和server response封装方法
This commit is contained in:
@ -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
|
||||
}
|
||||
}
|
||||
|
||||
@ -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()
|
||||
|
||||
Reference in New Issue
Block a user