-
Notifications
You must be signed in to change notification settings - Fork 0
/
19.5.go
54 lines (45 loc) · 916 Bytes
/
19.5.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
package main
import (
"fmt"
"io/ioutil"
"log"
"net/http"
"net/http/httputil"
"os"
"time"
)
func main() {
debug := os.Getenv("DEBUG")
client := &http.Client{
Timeout: 500 * time.Millisecond,
}
request, err := http.NewRequest("GET", "https://ifconfig.co", nil)
if err != nil {
log.Fatal(err)
}
request.Header.Add("Accept", "application/json")
if debug == "1" {
debugRequest, err := httputil.DumpRequestOut(request, true)
if err != nil {
log.Fatal(err)
}
fmt.Printf("%s", debugRequest)
}
response, err := client.Do(request)
if err != nil {
log.Fatal(err)
}
defer response.Body.Close()
if debug == "1" {
debugResponse, err := httputil.DumpResponse(response, true)
if err != nil {
log.Fatal(err)
}
fmt.Printf("%s", debugResponse)
}
body, err := ioutil.ReadAll(response.Body)
if err != nil {
log.Fatal(err)
}
fmt.Printf("%s", body)
}