-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathresponse.go
66 lines (54 loc) · 1.23 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
package goozzle
import (
"encoding/json"
"net/http"
)
// Response HTTP-response struct
type Response struct {
request *Request
status int
headers http.Header
cookies []*http.Cookie
body []byte
}
// Request returns initial request
func (r *Response) Request() *Request {
return r.request
}
// Status returns response status code
func (r *Response) Status() int {
return r.status
}
// Header returns response header by name
func (r *Response) Header(key string) string {
return r.headers.Get(key)
}
// Headers returns response headers map
func (r *Response) Headers() map[string]string {
return getHeaders(r.headers)
}
// Cookie returns response cookie by name
func (r *Response) Cookie(key string) string {
for _, cookie := range r.cookies {
if cookie.Name == key {
return cookie.Value
}
}
return ""
}
// Cookies returns response cookies slice
func (r *Response) Cookies() []*http.Cookie {
return r.cookies
}
// Body returns response body
func (r *Response) Body() []byte {
return r.body
}
// String returns response body as string
func (r *Response) String() string {
return string(r.Body())
}
// JSON unmarshal JSON response to struct
func (r *Response) JSON(v interface{}) error {
return json.Unmarshal(r.body, v)
}