-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhttpclient.go
61 lines (56 loc) · 1.36 KB
/
httpclient.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
package gohttpclient
import (
"bytes"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"log"
"net/http"
"time"
)
func Get(url string, params map[string]interface{}, timeout time.Duration) (string, error) {
if len(params) > 0 {
url += sp(params)
}
client := &http.Client{Timeout: timeout}
resp, err := client.Get(url)
if err != nil {
log.Fatal("http get request error")
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
return string(body), err
}
func Post(url string, contentType string, data interface{}, timeout time.Duration) (string, error) {
client := &http.Client{Timeout: timeout}
jsonStr, _ := json.Marshal(data)
resp, err := client.Post(url, contentType, bytes.NewBuffer(jsonStr))
if err != nil {
log.Fatal("http post request error")
}
defer resp.Body.Close()
result, err := ioutil.ReadAll(resp.Body)
return string(result), err
}
func PostForm(urlStr string, params map[string][]string) (string, error) {
resp, err := http.PostForm(urlStr, params)
if err != nil {
log.Fatal("http get request error")
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
return string(body), err
}
// sp
func sp(params map[string]interface{}) string {
if len(params) == 0 {
return ""
}
var paramStr = ""
for k, v := range params {
str := fmt.Sprintf("%v", v)
paramStr += k + "=" + str + "&"
}
return "?" + paramStr[0:len(paramStr)-1]
}