-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutility.go
42 lines (37 loc) · 958 Bytes
/
utility.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
package kukumailgo
import (
"io"
"net/http"
)
func (s *Session) get(url string, body io.Reader, header ...http.Header) (*http.Response, error) {
return s.req("GET", url, body, header...)
}
func (s *Session) post(url string, body io.Reader, header ...http.Header) (*http.Response, error) {
return s.req("POST", url, body, header...)
}
func (s *Session) req(method, url string, body io.Reader, header ...http.Header) (*http.Response, error) {
req, err := http.NewRequest(method, url, body)
if err != nil {
return nil, err
}
if len(header) > 0 {
req.Header = header[0]
}
req.AddCookie(&http.Cookie{
Name: "cookie_csrf_token",
Value: s.CsrfToken,
})
req.AddCookie(&http.Cookie{
Name: "cookie_sessionhash",
Value: s.SessionHash,
})
resp, err := s.client.Do(req)
if err != nil {
return nil, err
}
return resp, nil
}
func readBody(resp *http.Response) ([]byte, error) {
defer resp.Body.Close()
return io.ReadAll(resp.Body)
}