-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
85 lines (71 loc) · 1.92 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
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
package main
import (
"flag"
"fmt"
"io"
"log"
"net/http"
"net/http/httputil"
"os"
"strings"
)
var (
showHelp bool
outputFormat string
)
func main() {
routeURL := flag.String("url", "", "URL of API route")
method := flag.String("method", "", "Method of route")
requestBody := flag.String("body", "", "Request body for POST request")
requestHeaders := flag.String("header", "", "Request headers in the format 'key1:value1,key2:value2'")
flag.BoolVar(&showHelp, "help", false, "Show help message")
flag.BoolVar(&showHelp, "h", false, "Show help message")
flag.StringVar(&outputFormat, "output", "simple", "Output format [simple, detailed, full]")
flag.Parse()
if showHelp {
help()
os.Exit(1)
}
if flag.NFlag() == 0 {
fmt.Println("Use -help or -h to see the help message")
os.Exit(1)
}
client := &http.Client{
Transport: &TracingTransport{},
}
req, err := http.NewRequest(*method, *routeURL, nil)
if err != nil {
log.Fatal(err)
}
if *method == "POST" && *requestBody != "" {
req.Body = io.NopCloser(strings.NewReader(*requestBody))
}
if *requestHeaders != "" {
headers := parseHeaders(*requestHeaders)
for key, values := range headers {
for _, value := range values {
req.Header.Add(key, value)
}
}
}
resp, err := client.Do(req)
if err != nil {
log.Fatal(err)
}
dump, err := httputil.DumpResponse(resp, true)
if err != nil {
log.Fatal(err)
}
logRequest(req)
logResponse(resp, dump)
}
func help() {
fmt.Printf("usage: goat -url <URL> -method <METHOD> [OPTIONS]\n\n")
fmt.Printf("options:\n")
fmt.Printf("\t-url\t\tURL of API route\n")
fmt.Printf("\t-method\t\tmethod of route [GET, POST, PUT, DELETE, PATCH, HEAD, OPTIONS]\n")
fmt.Printf("\t-body\t\tbody for requests\n")
fmt.Printf("\t-header\t\trequest headers in the format 'key1:value1,key2:value2'\n")
fmt.Printf("\t-help / -h\tshow help message\n")
fmt.Printf("\t-output\t\toutput format [simple, detailed, full]\n")
}