-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathresponse.go
45 lines (39 loc) · 1.27 KB
/
response.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package go_requests
import (
"encoding/json"
"fmt"
"github.com/tidwall/gjson"
)
// Response 响应结构体
type Response struct {
StatusCode int `json:"status_code"` // 状态码
Reason string `json:"reason"` // 状态码说明
Elapsed float64 `json:"elapsed"` // 请求耗时(秒)
Content []byte `json:"content"` // 响应二进制内容
Text string `json:"text"` // 响应文本
Headers map[string]string `json:"headers"` // 响应头
Cookies map[string]string `json:"cookies"` // 响应Cookies
Request *Request `json:"request"` // 原始请求
}
func (res *Response) Json() map[string]interface{} {
data := make(map[string]interface{})
err := json.Unmarshal(res.Content, &data)
if err != nil {
fmt.Println("响应文本转map失败")
}
return data
}
func (res *Response) JsonArray() []map[string]interface{} {
var data []map[string]interface{}
err := json.Unmarshal(res.Content, &data)
if err != nil {
fmt.Println("响应文本转map失败")
}
return data
}
func (res *Response) Get(path string) gjson.Result {
return gjson.Get(res.Text, path)
}
func (res *Response) GetMany(path string) []gjson.Result {
return gjson.GetMany(res.Text, path)
}