-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
http.go
122 lines (94 loc) · 2.53 KB
/
http.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
package main
import (
"bytes"
"fmt"
"io/ioutil"
"mime/multipart"
"net/http"
"os"
"strings"
)
const methodPUT = http.MethodPut
const methodGET = http.MethodGet
const methodPOST = http.MethodPost
const connOKAY int8 = 0
const connBAD int8 = 1
const acceptMIME = "text/csv"
const acceptJSON = "application/json"
const acceptXMP = "application/rdf+xml"
func testConnection(request string) int8 {
conn := connOKAY
stream, err := http.NewRequest(methodGET, request, nil)
if err != nil {
fmt.Fprintln(os.Stderr, "ERROR: error creating request,", err)
os.Exit(1)
}
client := &http.Client{}
_, err = client.Do(stream)
if err != nil {
conn = connBAD
}
return conn
}
func handleConnection(stream *http.Request) string {
client := &http.Client{}
response, err := client.Do(stream)
if err != nil {
fmt.Fprintln(os.Stderr, "ERROR: Doing request,", err)
os.Exit(1)
}
data, err := ioutil.ReadAll(response.Body)
response.Body.Close()
if err != nil {
fmt.Fprintln(os.Stderr, "ERROR: Reading response body,", err)
os.Exit(1)
}
dataString := string(data)
trimmedResponse := strings.TrimSpace(dataString) //byte [] becomes string
return trimmedResponse
}
func makeMultipartConnection(VERB string, request string, fp *os.File, fname string, accepttype string) string {
//https://gist.github.com/mattetti/5914158/f4d1393d83ebedc682a3c8e7bdc6b49670083b84
fileContents, err := ioutil.ReadAll(fp)
if err != nil {
fmt.Fprintln(os.Stderr, err)
}
body := new(bytes.Buffer)
writer := multipart.NewWriter(body)
part, err := writer.CreateFormFile("file", fname)
if err != nil {
fmt.Fprintln(os.Stderr, err)
}
part.Write(fileContents)
err = writer.Close() //writer must be closed to avoid: [UNEXPECTED EOF]
if err != nil {
fmt.Fprintln(os.Stderr, err)
}
stream, err := http.NewRequest(VERB, request, body)
stream.Header.Add("Content-Type", writer.FormDataContentType())
if accepttype != "" {
stream.Header.Add("Accept", accepttype)
}
if err != nil {
fmt.Fprintln(os.Stderr, "ERROR: error creating request,", err)
os.Exit(1)
}
return handleConnection(stream)
}
func makeConnection(VERB string, request string, fp *os.File, accepttype string) string {
var stream *http.Request
var err error
if fp != nil {
stream, err = http.NewRequest(VERB, request, fp)
} else {
stream, err = http.NewRequest(VERB, request, nil)
}
if accepttype != "" {
stream.Header.Add("Accept", accepttype)
}
if err != nil {
fmt.Fprintln(os.Stderr, "ERROR: error creating request,", err)
os.Exit(1)
}
return handleConnection(stream)
}