-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathreq.go
59 lines (52 loc) · 927 Bytes
/
req.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
package steam
import (
"io/ioutil"
"net/http"
"strings"
)
type (
Req interface {
Get(urlStr string) (string, error)
Post(urlStr, body string) (string, error)
}
defReq struct {
c *http.Client
}
)
func (d *defReq) Get(urlStr string) (body string, err error) {
var (
rsp *http.Response
b []byte
)
rsp, err = http.Get(urlStr)
if err != nil {
return
}
defer rsp.Body.Close()
if b, err = ioutil.ReadAll(rsp.Body); err != nil {
return
}
body = string(b)
return
}
func (d *defReq) Post(urlStr, body string) (resBody string, err error) {
var (
rsp *http.Response
b []byte
)
rsp, err = http.Post(urlStr, "application/x-www-form-urlencoded", strings.NewReader(body))
if err != nil {
return
}
defer rsp.Body.Close()
if b, err = ioutil.ReadAll(rsp.Body); err != nil {
return
}
resBody = string(b)
return
}
func NewDefReq() *defReq {
return &defReq{
c: http.DefaultClient,
}
}