2023-05-18 11:01:34 +08:00
|
|
|
package utils
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"mime/multipart"
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
2023-06-17 09:38:26 +08:00
|
|
|
func HttpDo(reqUrl, method string, params map[string]interface{}, header map[string]string) (data []byte, err error) {
|
2023-05-18 11:01:34 +08:00
|
|
|
var paramStr string = ""
|
|
|
|
if contentType, ok := header["Content-Type"]; ok && strings.Contains(contentType, "json") {
|
|
|
|
bytesData, _ := json.Marshal(params)
|
|
|
|
paramStr = string(bytesData)
|
|
|
|
} else {
|
|
|
|
for k, v := range params {
|
|
|
|
if len(paramStr) == 0 {
|
2023-06-17 09:38:26 +08:00
|
|
|
paramStr = fmt.Sprintf("%s=%s", k, url.QueryEscape(v.(string)))
|
2023-05-18 11:01:34 +08:00
|
|
|
} else {
|
2023-06-17 09:38:26 +08:00
|
|
|
paramStr = fmt.Sprintf("%s&%s=%s", paramStr, k, url.QueryEscape(v.(string)))
|
2023-05-18 11:01:34 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
client := &http.Client{}
|
|
|
|
req, err := http.NewRequest(strings.ToUpper(method), reqUrl, strings.NewReader(paramStr))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
for k, v := range header {
|
|
|
|
req.Header.Set(k, v)
|
|
|
|
}
|
|
|
|
resp, err := client.Do(req)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
if resp.Body != nil {
|
|
|
|
err = resp.Body.Close()
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
var body []byte
|
|
|
|
if resp.Body != nil {
|
|
|
|
body, err = io.ReadAll(resp.Body)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return body, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type UploadFile struct {
|
|
|
|
// 表单名称
|
|
|
|
Name string
|
|
|
|
Filepath string
|
|
|
|
// 文件全路径
|
|
|
|
File *bytes.Buffer
|
|
|
|
}
|
|
|
|
|
|
|
|
func PostFile(reqUrl string, reqParams map[string]string, contentType string, files []UploadFile, headers map[string]string) string {
|
|
|
|
requestBody, realContentType := getReader(reqParams, contentType, files)
|
|
|
|
httpRequest, _ := http.NewRequest("POST", reqUrl, requestBody)
|
|
|
|
// 添加请求头
|
|
|
|
httpRequest.Header.Add("Content-Type", realContentType)
|
|
|
|
if headers != nil {
|
|
|
|
for k, v := range headers {
|
|
|
|
httpRequest.Header.Add(k, v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
httpClient := &http.Client{}
|
|
|
|
// 发送请求
|
|
|
|
resp, err := httpClient.Do(httpRequest)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
defer func(Body io.ReadCloser) {
|
|
|
|
_ = Body.Close()
|
|
|
|
}(resp.Body)
|
|
|
|
response, _ := io.ReadAll(resp.Body)
|
|
|
|
return string(response)
|
|
|
|
}
|
|
|
|
|
|
|
|
func getReader(reqParams map[string]string, contentType string, files []UploadFile) (io.Reader, string) {
|
|
|
|
if strings.Index(contentType, "json") > -1 {
|
|
|
|
bytesData, _ := json.Marshal(reqParams)
|
|
|
|
return bytes.NewReader(bytesData), contentType
|
|
|
|
} else if files != nil {
|
|
|
|
body := &bytes.Buffer{}
|
|
|
|
// 文件写入 body
|
|
|
|
writer := multipart.NewWriter(body)
|
|
|
|
for _, uploadFile := range files {
|
|
|
|
part, err := writer.CreateFormFile(uploadFile.Name, filepath.Base(uploadFile.Filepath))
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
_, err = io.Copy(part, uploadFile.File)
|
|
|
|
}
|
|
|
|
// 其他参数列表写入 body
|
|
|
|
for k, v := range reqParams {
|
|
|
|
if err := writer.WriteField(k, v); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if err := writer.Close(); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
// 上传文件需要自己专用的contentType
|
|
|
|
return body, writer.FormDataContentType()
|
|
|
|
} else {
|
|
|
|
urlValues := url.Values{}
|
|
|
|
for key, val := range reqParams {
|
|
|
|
urlValues.Set(key, val)
|
|
|
|
}
|
|
|
|
reqBody := urlValues.Encode()
|
|
|
|
return strings.NewReader(reqBody), contentType
|
|
|
|
}
|
|
|
|
}
|