-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathupload.go
53 lines (45 loc) · 1.02 KB
/
upload.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
package main
import (
"bytes"
"context"
"crypto/tls"
"log"
"net"
"net/http"
"time"
)
func UploadToCollector(url string, data []byte) (err error) {
var HTTPClient = &http.Client{
Timeout: time.Second * 30,
Transport: &http.Transport{
Dial: (&net.Dialer{
Timeout: 10 * time.Second,
}).Dial,
TLSHandshakeTimeout: 10 * time.Second,
TLSClientConfig: &tls.Config{
InsecureSkipVerify: true,
},
},
}
var resp *http.Response
var req *http.Request
ctx, cancel := context.WithTimeout(context.Background(), time.Second*15)
defer cancel()
req, err = http.NewRequestWithContext(ctx, "POST", url, bytes.NewReader(data))
if err != nil {
log.Println(" New request error:", err)
}
//for key, val := range Headers {
// if debug {
// fmt.Printf("Request Header: %s: %s\n", key, val)
// }
// req.Header.Set(key, val)
//}
resp, err = HTTPClient.Do(req)
if err != nil || resp.StatusCode != 200 {
log.Printf("response:\n %#v\nerror:\n %s\n", resp, err)
} else {
log.Println("...pushed")
}
return
}