-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.go
41 lines (35 loc) · 774 Bytes
/
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
package main
import (
"io"
"log"
"strings"
"github.com/alexflint/go-arg"
"github.com/knbr13/http-client/internal/httpmethods"
)
func main() {
input := httpmethods.Input{}
arg.MustParse(&input)
httpResponse, err := httpmethods.RunHttpMethod(input)
if err != nil {
log.Fatal(err)
}
defer httpResponse.Body.Close()
switch strings.ToLower(input.HTTPMethod) {
case "head":
// For HEAD request, only print the response status
printColoredHeaders(httpResponse.Header)
default:
// For other requests, print the response body
body, err := io.ReadAll(httpResponse.Body)
if err != nil {
log.Fatal(err)
}
printColoredBody(body)
if input.Output != "" {
err = writeToFile(input.Output, body)
if err != nil {
log.Fatal(err)
}
}
}
}