From 9ee887fce2f87f7a79d0b94640cf1d341a254319 Mon Sep 17 00:00:00 2001
From: zhangzengfei <zhangzengfei@smartai.com>
Date: 星期五, 17 五月 2024 02:55:14 +0800
Subject: [PATCH] 添加webapi, 优化模块启动
---
util/http.go | 32 +++++++++++++++++++++++++++++++-
1 files changed, 31 insertions(+), 1 deletions(-)
diff --git a/util/http.go b/util/http.go
index 7af0162..248aba4 100644
--- a/util/http.go
+++ b/util/http.go
@@ -2,9 +2,30 @@
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))
@@ -12,7 +33,7 @@
return nil, err
}
- req.Header.Set("Content-Type", "applicaiton/json; charset=UTF-8")
+ //req.Header.Set("Content-Type", "applicaiton/json; charset=UTF-8")
if header != nil {
for k, v := range header {
req.Header.Set(k, v)
@@ -33,3 +54,12 @@
return body, nil
}
+
+func HttpGet(url string) error {
+ _, err := http.Get(url)
+ if err != nil {
+ return err
+ }
+
+ return nil
+}
--
Gitblit v1.8.0