forked from yuanjack/champloo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.go
133 lines (117 loc) · 3.3 KB
/
util.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
package main
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io"
"io/ioutil"
"mime/multipart"
"net/http"
)
func HttpGet(url string) (string, int, error) {
resp, err := http.Get(url)
if err != nil {
return err.Error(), -1, err
}
body, err := ioutil.ReadAll(resp.Body)
resp.Body.Close()
if err != nil {
fmt.Printf("请求%s出错,错误消息:%v", url, err.Error())
return err.Error(), -1, err
}
return string(body), resp.StatusCode, nil
}
func PutJson(url string, data interface{}) (string, int, error) {
bytesData, _ := json.Marshal(data)
buf := bytes.NewReader(bytesData)
client := &http.Client{}
req, err := http.NewRequest("PUT", url, buf)
req.Header.Add("Content-Type", "application/json")
resp, err := client.Do(req)
if err != nil {
return err.Error(), -1, err
}
body, err := ioutil.ReadAll(resp.Body)
resp.Body.Close()
if err != nil {
fmt.Printf("请求%s出错,提交内容:%v,错误消息:%v", url, bytesData, err.Error())
return err.Error(), -1, err
}
return string(body), resp.StatusCode, nil
}
func PostJson(url string, data interface{}) (string, int, error) {
bytesData, _ := json.Marshal(data)
buf := bytes.NewReader(bytesData)
fmt.Println(string(bytesData))
resp, err := http.Post(url, "application/json", buf)
if err != nil {
return err.Error(), -1, err
}
body, err := ioutil.ReadAll(resp.Body)
resp.Body.Close()
if err != nil {
fmt.Printf("请求%s出错,提交内容:%v,错误消息:%v", url, bytesData, err.Error())
return err.Error(), -1, err
}
return string(body), resp.StatusCode, nil
}
func PostJsonWithFile(url string, data interface{}, fileParams map[string][]byte) (string, int, error) {
bodyBuf := &bytes.Buffer{}
bodyWriter := multipart.NewWriter(bodyBuf)
//关键的一步操作,创建一个multipart,用于写入文件数据
for filename, fileBytes := range fileParams {
fileWriter, err := bodyWriter.CreateFormFile(filename, filename)
if err != nil {
return err.Error(), -1, err
}
//写入文件数据到multipart
fileBuf := bytes.NewReader(fileBytes)
_, err = io.Copy(fileWriter, fileBuf)
if err != nil {
return err.Error(), -1, err
}
}
//写入json数据
fileWriter, err := bodyWriter.CreateFormField("json")
if err != nil {
return err.Error(), -1, err
}
bytesData, _ := json.Marshal(data)
jsonBuf := bytes.NewReader(bytesData)
_, err = io.Copy(fileWriter, jsonBuf)
if err != nil {
return err.Error(), -1, err
}
contentType := bodyWriter.FormDataContentType()
bodyWriter.Close()
resp, err := http.Post(url, contentType, bodyBuf)
if err != nil {
return err.Error(), -1, err
}
body, err := ioutil.ReadAll(resp.Body)
resp.Body.Close()
if err != nil {
fmt.Printf("请求%s出错,提交内容:%v,错误消息:%v", url, bytesData, err.Error())
return err.Error(), -1, err
}
return string(body), resp.StatusCode, nil
}
// 下载文件
func DownloadFileToBytes(url string) ([]byte, error) {
res, err := http.Get(url)
if err != nil {
fmt.Errorf(err.Error())
return []byte{}, err
}
bytes, err := ioutil.ReadAll(res.Body)
defer res.Body.Close()
if res.StatusCode != 200 {
return []byte{}, errors.New(fmt.Sprintf("请求出错.响应码:%d 响应内容:%s", res.StatusCode, string(bytes)))
}
if err != nil {
fmt.Errorf(err.Error())
return []byte{}, err
}
return bytes, err
}