修改http请求返回数据/钉钉消息通知文案修改
This commit is contained in:
37
libs/http.go
37
libs/http.go
@@ -8,24 +8,17 @@
|
||||
package libs
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"github.com/pkg/errors"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"strings"
|
||||
"net/http"
|
||||
"io"
|
||||
)
|
||||
|
||||
type AjaxReturn struct {
|
||||
Status int `json:"status"`
|
||||
Message string `json:"message"`
|
||||
Data interface{} `json:"data"`
|
||||
}
|
||||
|
||||
func HttpGet(url string, param map[string]string) error {
|
||||
func HttpGet(url string, param map[string]string) (string, error) {
|
||||
|
||||
if url == "" {
|
||||
return errors.Errorf("url %s is not exists", url)
|
||||
return "", errors.Errorf("url %s is not exists", url)
|
||||
}
|
||||
paramStr := ""
|
||||
for k, v := range param {
|
||||
@@ -40,7 +33,7 @@ func HttpGet(url string, param map[string]string) error {
|
||||
resp, err := http.Get(url)
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
return "", err
|
||||
}
|
||||
|
||||
defer resp.Body.Close()
|
||||
@@ -48,31 +41,27 @@ func HttpGet(url string, param map[string]string) error {
|
||||
body, err := ioutil.ReadAll(resp.Body)
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
return "", err
|
||||
}
|
||||
ajaxData := AjaxReturn{}
|
||||
json.Unmarshal(body, &ajaxData)
|
||||
if ajaxData.Status != 200 {
|
||||
return errors.Errorf("msg %s", ajaxData.Message)
|
||||
}
|
||||
return nil
|
||||
|
||||
return string(body), nil
|
||||
}
|
||||
|
||||
func HttpPost(url string, contentType string, body io.Reader) error {
|
||||
func HttpPost(url string, contentType string, body io.Reader) (string, error) {
|
||||
|
||||
resp, err := http.Post(url, contentType, body)
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
return "", err
|
||||
}
|
||||
|
||||
defer resp.Body.Close()
|
||||
|
||||
_, resErr := ioutil.ReadAll(resp.Body)
|
||||
resBody, err := ioutil.ReadAll(resp.Body)
|
||||
|
||||
if resErr != nil {
|
||||
return resErr
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
return nil
|
||||
return string(resBody), nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user