forked from totothink/antchain
-
Notifications
You must be signed in to change notification settings - Fork 0
/
request.go
66 lines (52 loc) · 1.36 KB
/
request.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
62
63
64
65
66
package antchain
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"github.com/tidwall/gjson"
)
// ParamsMap 参数 Map
type ParamsMap map[string]interface{}
func convertToParamsMap(params interface{}) (paramsMap ParamsMap) {
paramStr, _ := json.Marshal(params)
_ = json.Unmarshal(paramStr, ¶msMap)
return
}
// 生成 biz_content 业务字段
func generateBizContent(body interface{}) string {
bodyStr, _ := json.Marshal(body)
return string(bodyStr)
}
// 发送Post请求
func (c *Client) doRequest(path string, paramsMap ParamsMap) (data []byte, err error) {
// 发起请求
var (
resp *http.Response
)
paramsJson, err := json.Marshal(paramsMap)
if err != nil {
return
}
fullPath := c.Endpoint + path
fmt.Printf("doRequest path=%v param=%v\n", fullPath, string(paramsJson))
req, err := http.NewRequest("POST", fullPath, bytes.NewReader(paramsJson))
req.Header.Set("Content-Type", "application/json")
resp, err = c.httpClient.Do(req)
if err != nil {
panic(err)
}
defer resp.Body.Close()
data, _ = ioutil.ReadAll(resp.Body)
fmt.Printf("doRequest path=%v got response=%v\n", fullPath, string(data))
result := gjson.Get(string(data), "success")
if result.Bool() == false {
err = &AccessError{
message: gjson.Get(string(data), "data").String(),
}
return
}
data = []byte(gjson.Get(string(data), "data").String())
return
}