-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
main.go
41 lines (36 loc) · 814 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 (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
)
// HTTPBinResponse struct
type HTTPBinResponse struct {
Args map[string]string `json:"args"`
Headers map[string]string `json:"headers"`
Origin string `json:"origin"`
URL string `json:"url"`
}
func main() {
APIURL := "https://httpbin.org/get?arg1=one&arg2=two"
req, err := http.NewRequest(http.MethodGet, APIURL, nil)
if err != nil {
panic(err)
}
client := http.DefaultClient
resp, err := client.Do(req)
if err != nil {
panic(err)
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
panic(err)
}
var r HTTPBinResponse
json.Unmarshal(body, &r)
fmt.Printf("%#v\n\n", r)
fmt.Printf("%v\n\n", r.Headers["User-Agent"])
fmt.Printf("%v\n", r.Args["arg2"])
}