Skip to content

Commit 9f607c0

Browse files
author
Openset
committed
Add: client
1 parent d0e4614 commit 9f607c0

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed

internal/client/client.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package client
2+
3+
import (
4+
"errors"
5+
"io/ioutil"
6+
"net/http"
7+
"net/http/cookiejar"
8+
"strings"
9+
10+
"github.com/openset/leetcode/internal/base"
11+
)
12+
13+
var err error
14+
15+
func init() {
16+
http.DefaultClient.Jar, err = cookiejar.New(nil)
17+
base.CheckErr(err)
18+
http.DefaultClient.CheckRedirect = func(req *http.Request, via []*http.Request) error {
19+
req.Header.Set("Referer", req.URL.String())
20+
if len(via) >= 3 {
21+
return errors.New("stopped after 3 redirects")
22+
}
23+
return nil
24+
}
25+
}
26+
27+
func Get(url string) []byte {
28+
resp, err := http.Get(url)
29+
base.CheckErr(err)
30+
defer resp.Body.Close()
31+
body, err := ioutil.ReadAll(resp.Body)
32+
base.CheckErr(err)
33+
return body
34+
}
35+
36+
func PostJson(url, jsonStr string) []byte {
37+
resp, err := http.Post(url, "application/json", strings.NewReader(jsonStr))
38+
base.CheckErr(err)
39+
defer resp.Body.Close()
40+
body, err := ioutil.ReadAll(resp.Body)
41+
base.CheckErr(err)
42+
return body
43+
}

0 commit comments

Comments
 (0)