-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
57 lines (51 loc) · 1.15 KB
/
main.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
package main
import (
"bytes"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"strings"
)
func main() {
url := os.Getenv("URL")
if url == "" {
log.Fatal("url not defined")
}
method := os.Getenv("METHOD")
if method == "" {
method = "POST"
}
payload := os.Getenv("PAYLOAD")
post(url, method, payload)
}
func post(url string, method string, payload string) {
fmt.Println("{0} Performing Http {1}...", url, strings.ToUpper(method))
var data []byte
if (method == "post" || method == "put") && payload != "" {
data = []byte(payload)
}
client := &http.Client{}
var req *http.Request
if data != nil {
log.Print("With data")
req, _ = http.NewRequest(method, url, bytes.NewBuffer(data))
} else {
req, _ = http.NewRequest(method, url, nil)
}
if os.Getenv("AUTH") != "" {
req.Header.Set("Authorization", os.Getenv("AUTH"))
}
req.Header.Set("Content-Type", "application/json")
resp, err := client.Do(req)
if err != nil {
log.Fatalln(err)
}
defer resp.Body.Close()
bodyBytes, _ := ioutil.ReadAll(resp.Body)
// Convert response body to string
bodyString := string(bodyBytes)
fmt.Println(bodyString)
fmt.Println("status is", resp.StatusCode)
}