From c832144308869ebe389104a1e36392c3abc9f875 Mon Sep 17 00:00:00 2001 From: aozhiwei Date: Wed, 16 Dec 2020 17:55:55 +0800 Subject: [PATCH] add httppostcontent --- httpcli.go | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/httpcli.go b/httpcli.go index 919823d..ed0bdaa 100644 --- a/httpcli.go +++ b/httpcli.go @@ -4,6 +4,7 @@ import ( "net/http" "io/ioutil" "errors" + "strings" ) func HttpGet(url string, params *XObject) (string, error) { @@ -14,6 +15,7 @@ func HttpGet(url string, params *XObject) (string, error) { if resp.StatusCode != 200 { return "", errors.New("HttpGet error Status:" + resp.Status) } + defer resp.Body.Close() if bytes, err := ioutil.ReadAll(resp.Body); err == nil { return string(bytes), nil } else { @@ -35,3 +37,16 @@ func HttpGetAsJson(url string, params *XObject) (*XObject, string, error) { } return respObj, respStr, err } + +func HttpPostContent(url string, contentType string, body string) (string, error) { + resp, err := http.Post(url, contentType, strings.NewReader(body)) + if err != nil { + return "", err + } + defer resp.Body.Close() + if bytes, err := ioutil.ReadAll(resp.Body); err == nil { + return string(bytes), nil + } else { + return "", err + } +}