Use Accept header for returning JSON instead of plain text

This commit is contained in:
Paul Brinkmeier 2024-07-18 21:41:06 +02:00
parent 56739125b3
commit cc11b626b6

View File

@ -177,6 +177,12 @@ func main() {
return return
} }
// Does not handle multiple media types
if r.Header.Get("Accept") == "application/json" {
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(departures)
} else {
// plain text
for _, d := range departures.Departures { for _, d := range departures.Departures {
fmt.Fprintf(w, "%2s %-11s %6s\n", fmt.Fprintf(w, "%2s %-11s %6s\n",
d.Symbol, d.Symbol,
@ -184,6 +190,7 @@ func main() {
d.Leaving, d.Leaving,
) )
} }
}
}) })
log.Fatal(http.ListenAndServe(":8000", nil)) log.Fatal(http.ListenAndServe(":8000", nil))
} }