This repository was archived by the owner on Oct 25, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathutil_http.go
More file actions
69 lines (58 loc) · 1.29 KB
/
util_http.go
File metadata and controls
69 lines (58 loc) · 1.29 KB
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
package libwimark
import (
"bytes"
"io"
"mime/multipart"
"net/http"
"os"
"path/filepath"
)
func SendHTTPPost(url, mime string, request []byte) ([]byte, error) {
if len(mime) == 0 {
mime = "application/json"
}
resp, err := http.Post(url, mime, bytes.NewBuffer(request))
if err != nil {
return []byte{}, err
}
defer resp.Body.Close()
return io.ReadAll(resp.Body)
}
func SendHTTPGet(url string) ([]byte, error) {
resp, err := http.Get(url)
if err != nil {
return []byte{}, err
}
defer resp.Body.Close()
return io.ReadAll(resp.Body)
}
func SendHTTPPostFile(url, filename, filetype string) ([]byte, error) {
file, err := os.Open(filename)
if err != nil {
return []byte{}, err
}
defer file.Close()
body := &bytes.Buffer{}
writer := multipart.NewWriter(body)
part, err := writer.CreateFormFile(filetype, filepath.Base(file.Name()))
if err != nil {
return []byte{}, err
}
_, err = io.Copy(part, file)
if err != nil {
return []byte{}, err
}
writer.Close()
request, err := http.NewRequest("POST", url, body)
if err != nil {
return []byte{}, err
}
request.Header.Add("Content-Type", writer.FormDataContentType())
client := &http.Client{}
resp, err := client.Do(request)
if err != nil {
return []byte{}, err
}
defer resp.Body.Close()
return io.ReadAll(resp.Body)
}