You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
SOP/sop-sdk/sdk-go/common/HttpTool.go

128 lines
3.2 KiB

5 years ago
package common
import (
"bytes"
"encoding/json"
"io"
"io/ioutil"
"mime/multipart"
"net/http"
"net/url"
"os"
"path/filepath"
"strings"
)
type UploadFile struct {
// 表单名称
Name string
5 years ago
// 文件全路径
5 years ago
Filepath string
}
// 请求客户端
var httpClient = &http.Client{}
5 years ago
func Get(reqUrl string, reqParams map[string]string, headers map[string]string) string {
5 years ago
urlParams := url.Values{}
Url, _ := url.Parse(reqUrl)
5 years ago
for key, val := range reqParams {
5 years ago
urlParams.Set(key, val)
}
//如果参数中有中文参数,这个方法会进行URLEncode
Url.RawQuery = urlParams.Encode()
// 得到完整的url,http://xx?query
urlPath := Url.String()
httpRequest,_ := http.NewRequest("GET", urlPath, nil)
// 添加请求头
if headers != nil {
for k, v := range headers {
httpRequest.Header.Add(k,v)
}
}
// 发送请求
resp, err := httpClient.Do(httpRequest)
if err != nil {
panic(err)
}
defer resp.Body.Close()
response, _ := ioutil.ReadAll(resp.Body)
return string(response)
}
5 years ago
func PostForm(reqUrl string, reqParams map[string]string, headers map[string]string) string {
return post(reqUrl, reqParams, "application/x-www-form-urlencoded", nil, headers)
5 years ago
}
5 years ago
func PostJson(reqUrl string, reqParams map[string]string, headers map[string]string) string {
return post(reqUrl, reqParams, "application/json", nil, headers)
5 years ago
}
5 years ago
func PostFile(reqUrl string, reqParams map[string]string, files []UploadFile, headers map[string]string) string {
return post(reqUrl, reqParams, "multipart/form-data", files, headers)
5 years ago
}
5 years ago
func post(reqUrl string, reqParams map[string]string, contentType string, files []UploadFile, headers map[string]string) string {
requestBody, realContentType := getReader(reqParams, contentType, files)
5 years ago
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)
}
}
// 发送请求
resp, err := httpClient.Do(httpRequest)
if err != nil {
panic(err)
}
defer resp.Body.Close()
response, _ := ioutil.ReadAll(resp.Body)
return string(response)
}
5 years ago
func getReader(reqParams map[string]string, contentType string, files []UploadFile) (io.Reader, string) {
5 years ago
if strings.Index(contentType, "json") > -1 {
5 years ago
bytesData, _ := json.Marshal(reqParams)
5 years ago
return bytes.NewReader(bytesData), contentType
} else if files != nil {
body := &bytes.Buffer{}
// 文件写入 body
writer := multipart.NewWriter(body)
for _, uploadFile := range files {
file, err := os.Open(uploadFile.Filepath)
if err != nil {
panic(err)
}
part, err := writer.CreateFormFile(uploadFile.Name, filepath.Base(uploadFile.Filepath))
if err != nil {
panic(err)
}
_, err = io.Copy(part, file)
file.Close()
}
// 其他参数列表写入 body
5 years ago
for k, v := range reqParams {
5 years ago
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{}
5 years ago
for key, val := range reqParams {
5 years ago
urlValues.Set(key, val)
}
reqBody:= urlValues.Encode()
return strings.NewReader(reqBody), contentType
}
}