-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathhttp.go
45 lines (36 loc) · 917 Bytes
/
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
package krakenapi
import (
"fmt"
"io"
"io/ioutil"
"net/http"
"net/url"
"strings"
)
func executeHttpQuery(method string, url string, headers map[string]string, values url.Values) ([]byte, error) {
var bodyReader io.Reader
client := &http.Client{}
if method == "GET" {
bodyReader = nil
url = url + "?" + values.Encode()
} else {
bodyReader = strings.NewReader(values.Encode())
}
req, err := http.NewRequest(method, url, bodyReader)
if err != nil {
return nil, fmt.Errorf("Could not execute request! (%s)", err.Error())
}
for key, value := range headers {
req.Header.Add(key, value)
}
resp, err := client.Do(req)
if err != nil {
return nil, fmt.Errorf("Could not execute request! (%s)", err.Error())
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, fmt.Errorf("Could not execute request! (%s)", err.Error())
}
return body, nil
}