hpds_jkw_web/pkg/minedata/httpUtils.go

41 lines
847 B
Go
Raw Normal View History

2023-01-11 18:05:29 +08:00
package minedata
import (
"fmt"
"io"
"net/http"
"net/url"
"strings"
)
func HttpDo(reqUrl, method string, params map[string]string, header map[string]string) (data []byte, err error) {
var paramStr = ""
for k, v := range params {
if len(paramStr) == 0 {
paramStr = fmt.Sprintf("%s=%s", k, url.QueryEscape(v))
} else {
paramStr = fmt.Sprintf("%s&%s=%s", paramStr, k, url.QueryEscape(v))
}
}
client := &http.Client{}
req, err := http.NewRequest(strings.ToUpper(method), reqUrl, strings.NewReader(paramStr))
if err != nil {
return nil, err
}
req.Header.Set("content-type", "X-WWW-FORM-URLENCODED")
for k, v := range header {
req.Header.Set(k, v)
}
resp, err := client.Do(req)
defer func() {
_ = resp.Body.Close()
}()
body, err := io.ReadAll(resp.Body)
if err != nil {
return nil, err
}
return body, nil
}