/
response.go
85 lines (71 loc) · 2.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
// Package http
// @Title response.go
// @Description
// @Author haogooder
// @Update 2022/2/8
package http
import "net/http"
func NewHttpResponse(resp *http.Response) *HttpResponse {
return &HttpResponse{
header: make(map[string]string),
response: resp,
}
}
// 响应结构体,在response基础上封装
type HttpResponse struct {
body []byte // 响应的body
status string // 响应码的描述信息,"200 OK"
statusCode int // 响应码"200"
proto string // 所用协议"HTTP/1.1"
header map[string]string // 头信息
contentLen int64 // 返回内容长度
setCookie []http.Cookie // 要设置的cookie信息
location string // 当statusCode为3XX如301时重定向的链接
err error // 请求的出错信息
transferEncoding []string // 所用的编码信息
response *http.Response // 原始的响应信息
}
// 提取body信息
func (tfr HttpResponse) GetBody() []byte {
return tfr.body
}
// 返回响应的body的字符串格式
func (tfr HttpResponse) GetBodyString() string {
return string(tfr.body)
}
// 提取状态描述信息,如"200 OK"
func (tfr HttpResponse) GetStatus() string {
return tfr.status
}
// 提取状态码,如200
func (tfr HttpResponse) GetStatusCode() int {
return tfr.statusCode
}
// 所用协议"HTTP/1.1"
func (tfr HttpResponse) GetProto() string {
return tfr.proto
}
// 提取响应的头信息
func (tfr HttpResponse) GetHeader() map[string]string {
return tfr.header
}
// 获取响应信息的长度
func (tfr HttpResponse) GetContentLen() int64 {
return tfr.contentLen
}
// 获取响应头里面set-cookie设置cookie信息的列表
func (tfr HttpResponse) GetSetCookie() []http.Cookie {
return tfr.setCookie
}
// 获取重定向后的地址信息
func (tfr HttpResponse) GetLocation() string {
return tfr.location
}
// 提取响应的编码信息
func (tfr HttpResponse) GetTransferEncoding() []string {
return tfr.transferEncoding
}
// 返回出错的信息
func (tfr HttpResponse) Error() error {
return tfr.err
}