/
response.go
136 lines (123 loc) · 2.71 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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
package hrq
import (
"bytes"
"compress/gzip"
"encoding/json"
"io/ioutil"
"net/http"
"net/http/httputil"
"net/url"
"strings"
"golang.org/x/net/html/charset"
)
// Response inherits http.Response.
type Response struct {
*http.Response
// History is the redirect history.
History []*http.Request
rawBody []byte
}
// URL returns a request url.
func (r *Response) URL() *url.URL {
return r.Response.Request.URL
}
// CookieValue returns a cookie value.
func (r *Response) CookieValue(name string) string {
lowerName := strings.ToLower(name)
for _, c := range r.Response.Cookies() {
if strings.ToLower(c.Name) == lowerName {
return c.Value
}
}
return ""
}
// CookiesMap returns the response cookies by map.
func (r *Response) CookiesMap() map[string]string {
cookies := map[string]string{}
for _, c := range r.Response.Cookies() {
cookies[c.Name] = c.Value
}
return cookies
}
// DumpHeader returns a header string.
func (r *Response) DumpHeader() (dump string, err error) {
b, err := httputil.DumpResponse(r.Response, false)
return string(b), err
}
// HeaderValue returns header value.
func (r *Response) HeaderValue(name string) string {
lowerName := strings.ToLower(name)
for k, v := range r.Header {
if strings.ToLower(k) == lowerName {
return v[0]
}
}
return ""
}
// Content returns response body by byte.
func (r *Response) Content() (bs []byte, err error) {
if r.rawBody != nil {
return r.rawBody, nil
}
defer r.Body.Close()
encoding := r.HeaderValue("Content-Encoding")
body := r.Body
if encoding == "gzip" {
body, err = gzip.NewReader(r.Body)
if err != nil {
return nil, err
}
defer body.Close()
}
bs, err = ioutil.ReadAll(body)
if err != nil {
return nil, err
}
r.rawBody = bs
return bs, err
}
// ContentType returns content-type in response header..
func (r *Response) ContentType() string {
return r.HeaderValue("Content-Type")
}
// Encode returns encode of response body.
func (r *Response) Encode() (encode string, err error) {
contentType := r.ContentType()
body, err := r.Content()
if err != nil {
return
}
_, encode, _ = charset.DetermineEncoding(body, contentType)
return
}
// Text returns response body by string.
func (r *Response) Text() (text string, err error) {
encode, err := r.Encode()
if err != nil {
return
}
content, err := r.Content()
if err != nil {
return
}
br := bytes.NewReader(content)
rl, err := charset.NewReaderLabel(encode, br)
if err != nil {
return
}
bs, err := ioutil.ReadAll(rl)
if err != nil {
return
}
text = string(bs)
return
}
// JSON returns unmarshal response body.
func (r *Response) JSON(t interface{}) error {
rawBody, err := r.Content()
if err != nil {
return err
}
err = json.Unmarshal(rawBody, t)
return err
}