-
Notifications
You must be signed in to change notification settings - Fork 0
/
servicenowhttp.go
60 lines (51 loc) · 1.44 KB
/
servicenowhttp.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
package main
import (
"crypto/tls"
"encoding/csv"
"io/ioutil"
"log"
"net/http"
"strings"
)
func snhttp(url string) [][]string {
// GET CONFIG
config, _ := parseConfig()
// CREATE HTTP CLIENT
tr := &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
}
client := &http.Client{Transport: tr}
req, err := http.NewRequest("GET", url, nil)
if err != nil {
log.Fatal(err)
}
// SET BASIC AUTH
req.SetBasicAuth(config.ServiceNow.User, config.ServiceNow.Password)
// MAKE HTTP REQUEST
if config.Logging.verbose == true {
log.Printf("DEBUG - Making ServiceNow API call ...\r\n")
}
resp, err := client.Do(req)
// LOG SERVICE NOW HTTP REQUEST
if config.Logging.verbose == true {
log.Printf("DEBUG - ServiceNow API HTTP Request: %s %v \r\n", resp.Request.Method, resp.Request.URL)
log.Printf("DEBUG - ServiceNow API HTTP Reqest Header: %v \r\n", resp.Request.Header)
log.Printf("DEBUG - ServiceNow API Response Status Code: %d \r\n", resp.StatusCode)
}
if err != nil {
log.Fatal(err)
}
defer resp.Body.Close()
// READ IN CSV DATA
bodyBytes, _ := ioutil.ReadAll(resp.Body)
bodyString := string(bodyBytes)
reader := csv.NewReader(strings.NewReader(bodyString))
data, err := reader.ReadAll()
if config.Logging.verbose == true {
log.Printf("DEBUG - ServiceNowAPI Response CSV Data:\r\n %v \r\n", strings.Replace(bodyString, "\n", "\r\n", -1))
}
if err != nil {
log.Fatalf("ERROR - %s", err)
}
return data
}