Skip to content

Commit f960b73

Browse files
committed
添加直接json调用
1 parent 7e5494e commit f960b73

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

http/client/client.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package client
22

33
import (
44
"bytes"
5+
"encoding/json"
56
"fmt"
67
"io/ioutil"
78
"net"
@@ -84,6 +85,22 @@ func (c httpClient) POST(url string, headers map[string]string, body []byte) ([]
8485
return c.do("POST", url, headers, body)
8586
}
8687

88+
//PostJSON 传json结构数据.
89+
func (c httpClient) PostJSON(url string, headers map[string]string, data interface{}, resp interface{}) error {
90+
buf, err := json.Marshal(data)
91+
if err != nil {
92+
return errors.Trace(err)
93+
}
94+
95+
headers["Content-type"] = "application/json"
96+
97+
if buf, err = c.do("POST", url, headers, buf); err != nil {
98+
return errors.Trace(err)
99+
}
100+
101+
return json.Unmarshal(buf, resp)
102+
}
103+
87104
func (c httpClient) PUT(url string, headers map[string]string, body []byte) ([]byte, error) {
88105
return c.do("PUT", url, headers, body)
89106
}

0 commit comments

Comments
 (0)