I am working on a project I've tasked to myself.
I'm trying to set up a Golang server that displays JSON data from a remote json file based on a parameter (ID) passed by the user.
I can find plenty of guides that show how to consume ALL the data from the json API, but I don't know how to pass a parameter to the function so it only returns the required data.
Here is what I have so far:
Setting up the basic server and routes
func main() {
//Initialises basic router and endpoints
r := mux.NewRouter()
r.HandleFunc("/", home).Methods("GET")
r.HandleFunc("/games/all", getAll).Methods("GET")
r.HandleFunc("/games/{id}", getGame).Methods("POST")
r.HandleFunc("/games/report", getReport).Methods("GET")
fmt.Println("Listening on port 8080")
http.ListenAndServe(":8080", r)
}
This is my getAll method that sends the GET request (Using the pokedex api for now until i host my json file).
func getAll(w http.ResponseWriter, r *http.Request) {
response, err := http.Get("http://pokeapi.co/api/v2/pokedex/kanto/")
if err != nil {
fmt.Print(err.Error())
os.Exit(1)
}
responseData, err := ioutil.ReadAll(response.Body)
if err != nil {
log.Fatal(err)
}
fmt.Println(string(responseData))
}
Have also built the structs for the data. But i cant, for the life of me, find a way to pass the parameter to do the getGame method to handle that route. Please, if anybody could point me in the right direction.