|
@@ -6,6 +6,8 @@ import (
|
|
|
"crypto/tls"
|
|
|
"encoding/base64"
|
|
|
"encoding/json"
|
|
|
+ "git.listensoft.net/tool/jspkit/logger"
|
|
|
+ "go.uber.org/zap"
|
|
|
"io"
|
|
|
"net/http"
|
|
|
"net/http/cookiejar"
|
|
@@ -190,14 +192,43 @@ func Get(c *http.Client, url string) ([]byte, error) {
|
|
|
return io.ReadAll(resp.Body)
|
|
|
}
|
|
|
|
|
|
+
|
|
|
+func PostJson(url string, data map[string]interface{}, v any) error {
|
|
|
+ bytesData, _ := json.Marshal(data)
|
|
|
+ request, err := http.NewRequest("POST", url, bytes.NewReader(bytesData))
|
|
|
+ if err != nil {
|
|
|
+ logger.GetLogger().Info("err", zap.Error(err))
|
|
|
+ return err
|
|
|
+ }
|
|
|
+ request.Header.Set("Content-Type", "application/json;charset=UTF-8")
|
|
|
+ client := NewHttpClient()
|
|
|
+ resp, err := client.Do(request)
|
|
|
+ if err != nil {
|
|
|
+ return err
|
|
|
+ }
|
|
|
+ defer resp.Body.Close()
|
|
|
+
|
|
|
+ body, err := io.ReadAll(resp.Body)
|
|
|
+ if err != nil {
|
|
|
+ return err
|
|
|
+ }
|
|
|
+ if err = json.Unmarshal(body, v); err != nil {
|
|
|
+ return err
|
|
|
+ }
|
|
|
+ return nil
|
|
|
+}
|
|
|
+
|
|
|
|
|
|
-func NewHttpClient() *http.Client {
|
|
|
+func NewHttpClient(tt ...int) *http.Client {
|
|
|
+ if len(tt) == 0 {
|
|
|
+ tt = append(tt, 60)
|
|
|
+ }
|
|
|
tr := &http.Transport{
|
|
|
|
|
|
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
|
|
|
}
|
|
|
var c = &http.Client{
|
|
|
- Timeout: time.Duration(60 * time.Second),
|
|
|
+ Timeout: time.Duration(tt[0]) * time.Second,
|
|
|
Transport: tr,
|
|
|
}
|
|
|
options := cookiejar.Options{
|