-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpost_to_server.go
53 lines (49 loc) · 1.18 KB
/
post_to_server.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
package util
import (
"encoding/json"
"io"
"net/http"
"strings"
"time"
)
func PostToServer(serverApiUrl, serverToken string, postData interface{}) (ret []byte, err error) {
jsonParams, err := json.Marshal(postData)
if err != nil {
return
}
jsonString := string(jsonParams)
payload := strings.NewReader(jsonString)
apiUrl := serverApiUrl
req, err := http.NewRequest("POST", apiUrl, payload)
if err != nil {
return
}
req.Header.Add("content-type", "application/json")
req.Header.Add("cache-control", "no-cache")
req.Header.Add("apiclient", serverToken) //接收 c.Request.Header["Apiclient"]
// req.Header.Add("User-Agent", "apisender")
client := http.Client{
Timeout: time.Second * 30,
}
resp, err := client.Do(req)
if err != nil {
return
}
defer resp.Body.Close()
return io.ReadAll(resp.Body)
// body, err := io.ReadAll(resp.Body)
// if err != nil {
// return
// }
// // fmt.Println(res)
// // ret = string(body)
// return body, nil
}
func PostToServerEx(serverApiUrl, serverToken string, postData interface{}) (ret string, err error) {
body, err := PostToServer(serverApiUrl, serverToken, postData)
if err != nil {
return
}
ret = string(body)
return
}