/
response.go
65 lines (50 loc) · 1.06 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
package goHttpProMaxPlus
import (
"encoding/json"
"io"
"io/ioutil"
"net/http"
)
type HttpResponse struct {
resp *http.Response
Headers map[string]string
Body io.ReadCloser
bodyStr *string
}
func (hr *HttpResponse) GetRespStr() string {
if hr.bodyStr == nil {
hr.setBodyStr()
}
return *hr.bodyStr
}
// GetResponse It return http.Response struct
func (hr *HttpResponse) GetResponse() *http.Response {
return hr.resp
}
func (hr *HttpResponse) ParseJson(v interface{}) {
_ = json.Unmarshal([]byte(hr.GetRespStr()), &v)
}
func CreateResponse(resp *http.Response) *HttpResponse{
response := &HttpResponse{
resp: resp,
}
response.parseHeader()
response.parseBody()
return response
}
func (hr *HttpResponse) setBodyStr() {
defer hr.Body.Close()
body, _ := ioutil.ReadAll(hr.Body)
_s := string(body)
hr.bodyStr = &_s
}
func (hr *HttpResponse) parseHeader() {
hr.Headers = map[string]string{}
header := hr.resp.Header
for k, _ := range header {
hr.Headers[k] = header.Get(k)
}
}
func (hr *HttpResponse) parseBody() {
hr.Body = hr.resp.Body
}