-
Notifications
You must be signed in to change notification settings - Fork 0
/
apiGET02.go
80 lines (67 loc) · 2.43 KB
/
apiGET02.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
/* Alta3 Research | RZFeeser
API - RESTful API lookup, and decoding JSON on 200 response */
package main
import (
"encoding/json"
"fmt"
"log"
"net/http"
"time"
)
type SpaceX []struct {
CapsuleSerial string `json:"capsule_serial"`
CapsuleID string `json:"capsule_id"`
Status string `json:"status"`
OriginalLaunch time.Time `json:"original_launch"`
OriginalLaunchUnix int `json:"original_launch_unix"`
Missions []struct {
Name string `json:"name"`
Flight int `json:"flight"`
} `json:"missions"`
Landings int `json:"landings"`
Type string `json:"type"`
Details string `json:"details"`
ReuseCount int `json:"reuse_count"`
}
func main() {
// define our URL (API) as a string
url := "https://api.spacexdata.com/v3/capsules"
// Build the request
req, err := http.NewRequest("GET", url, nil)
if err != nil {
log.Fatal("NewRequest: ", err)
return
}
// For control over HTTP client headers,
// redirect policy, and other settings,
// create a Client
// A Client is an HTTP client - you can load it up with
// parameters if you wanted (timeouts, tls, retries, etc.)
// You only need ONE of these (supported by goroutines)
client := &http.Client{}
// Send the request via a client
// Do sends an HTTP request and
// returns an HTTP response
resp, err := client.Do(req)
if err != nil {
log.Fatal("Do: ", err)
return
}
// Callers should close resp.Body
// when done reading from it
// Defer the closing of the body
defer resp.Body.Close()
// Fill the record with the data from the JSON
var record SpaceX
// Use json.Decode for reading streams of JSON data
if err := json.NewDecoder(resp.Body).Decode(&record); err != nil {
log.Println(err)
}
// the _ is an int
// launchData is the data
for launchNo, launchData := range record {
fmt.Println("Capsule Record =\n", launchData)
fmt.Println("Record Number =", launchNo)
fmt.Println("Capsule Serial =", launchData.CapsuleSerial)
}
}