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_https.go
More file actions
143 lines (126 loc) · 3.14 KB
/
Copy pathutil_https.go
File metadata and controls
143 lines (126 loc) · 3.14 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
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
134
135
136
137
138
139
140
141
142
143
package libwimark
import (
"bytes"
"crypto/tls"
"fmt"
"io"
"mime/multipart"
"net/http"
"os"
"path/filepath"
)
// SendHTTPSGet function to get from remote https URL
func SendHTTPSGet(url, bearerKey string) ([]byte, error) {
res := []byte{}
tr := &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
}
client := &http.Client{Transport: tr}
req, _ := http.NewRequest("GET", url, nil)
if len(bearerKey) > 0 {
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", bearerKey))
}
resp, err := client.Do(req)
if err != nil {
return res, err
}
defer resp.Body.Close()
if resp.StatusCode == http.StatusOK {
res, err = io.ReadAll(resp.Body)
if err != nil {
return res, err
}
} else {
return res, fmt.Errorf("bad status code: %+v", resp.StatusCode)
}
return res, nil
}
// SendHTTPSRequest function to make a request to remote URL
func SendHTTPSRequest(url, bearerKey,
method string, body []byte, contentType string) ([]byte, error) {
res := []byte{}
tr := &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
}
client := &http.Client{Transport: tr}
req, _ := http.NewRequest(method, url, bytes.NewBuffer(body))
if len(bearerKey) > 0 {
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", bearerKey))
}
switch contentType {
case "plain":
req.Header.Set("Content-Type", "text/plain")
case "", "json":
req.Header.Set("Content-Type", "application/json")
default:
req.Header.Set("Content-Type", contentType)
}
resp, err := client.Do(req)
if err != nil {
return res, err
}
defer resp.Body.Close()
if resp.StatusCode == http.StatusOK {
res, err = io.ReadAll(resp.Body)
if err != nil {
return res, err
}
} else {
return res, fmt.Errorf("bad status code: %+v", resp.StatusCode)
}
return res, nil
}
// SendHTTPSFileRequest function to POST file from provided PATH
func SendHTTPSFileRequest(url, bearerKey string, path string) ([]byte, error) {
res := []byte{}
file, err := os.Open(path)
if err != nil {
return res, err
}
defer file.Close()
body := &bytes.Buffer{}
writer := multipart.NewWriter(body)
part, err := writer.CreateFormFile("file", filepath.Base(path))
if err != nil {
return res, err
}
_, err = io.Copy(part, file)
if err != nil {
return res, err
}
err = writer.Close()
if err != nil {
return res, err
}
return SendHTTPSRequest(url, bearerKey, "POST", body.Bytes(), writer.FormDataContentType())
}
// SendHTTPSFileDataRequest function to POST file & data
func SendHTTPSFileDataRequest(url, bearerKey string, path string, data []byte) ([]byte, error) {
res := []byte{}
file, err := os.Open(path)
if err != nil {
return res, err
}
defer file.Close()
body := &bytes.Buffer{}
writer := multipart.NewWriter(body)
if len(data) > 0 {
err = writer.WriteField("data", string(data))
if err != nil {
return res, err
}
}
part, err := writer.CreateFormFile("file", filepath.Base(path))
if err != nil {
return res, err
}
_, err = io.Copy(part, file)
if err != nil {
return res, err
}
err = writer.Close()
if err != nil {
return res, err
}
return SendHTTPSRequest(url, bearerKey, "POST", body.Bytes(), writer.FormDataContentType())
}