-
Notifications
You must be signed in to change notification settings - Fork 1
/
http.go
82 lines (60 loc) · 1.5 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
package chttp
import (
"bytes"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net/http"
"time"
)
// Sends API request to Pterodactyl API (or perhaps technically any endpoint with /api/ :)).
func SendHTTPReq(endpoint string, auth string, request_type string, form_data map[string]interface{}) (string, int, error) {
// Initialize data and return code (status code).
d := ""
rc := -1
var post_body io.Reader
// Check to see if we need to send post data.
if request_type == "POST" || request_type == "PUT" {
// Convert to JSON and use as body.
j, err := json.Marshal(form_data)
if err != nil {
return d, rc, err
}
// Read byte array into IO reader.
post_body = bytes.NewBuffer(j)
if err != nil {
return d, rc, err
}
}
// Setup HTTP GET request.
client := &http.Client{Timeout: time.Second * 5}
req, err := http.NewRequest(request_type, endpoint, post_body)
if err != nil {
fmt.Println(err)
return d, rc, err
}
// Set Application API token.
req.Header.Set("Authorization", auth)
// Accept only JSON.
req.Header.Set("Accept", "application/json")
// Set content stype.
req.Header.Set("Content-Type", "application/json")
// Perform HTTP request and check for errors.
resp, err := client.Do(req)
if err != nil {
fmt.Println(err)
return d, rc, err
}
// Set return code.
rc = resp.StatusCode
// Read body.
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
fmt.Println(err)
return d, rc, err
}
// Return data as a string.
d = string(body)
return d, rc, nil
}