zhangzengfei
2024-04-16 bbcf45df17344e01bf75a589c17d3fcf7e7cc8da
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
package util
 
import (
    "bytes"
    "encoding/json"
    "io"
    "net/http"
 
    "gat1400Exchange/pkg/logger"
    "gat1400Exchange/vo"
)
 
func SendData(payload []byte, url string) bool {
    body, err := HttpPost(url, nil, payload)
    if err != nil {
        logger.Error("Post request failure, url:%s, err:%s", url, err.Error())
        return false
    }
 
    var rsp vo.SyncServerResponse
    err = json.Unmarshal(body, &rsp)
    if err != nil {
        logger.Error("Can't parse response, url:%s, rsp: %v", url, body)
        return false
    }
 
    return rsp.Success
}
 
func HttpPost(url string, header map[string]string, data []byte) ([]byte, error) {
    req, err := http.NewRequest(http.MethodPost, url, bytes.NewBuffer(data))
    if err != nil {
        return nil, err
    }
 
    req.Header.Set("Content-Type", "applicaiton/json; charset=UTF-8")
    if header != nil {
        for k, v := range header {
            req.Header.Set(k, v)
        }
    }
 
    cli := &http.Client{}
    resp, err := cli.Do(req)
    if err != nil {
        return nil, err
    }
 
    defer resp.Body.Close()
    body, err := io.ReadAll(resp.Body)
    if err != nil {
        return nil, err
    }
 
    return body, nil
}