Click here to Skip to main content
15,893,161 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I've got a Golang Website where I want to display 'scores' from my UWP Game using SQLite's Mobile App Quickstart's API called SwaggerUI. I am getting the scores by doing a HTTP GET request. The problem is that the scores output to the Golang console in JSON Format. I want to display the scores onto the actual website. How could I call my golang function in the Frontend in order to do this? The frontend is written in HTML/Javascript/JQuery.

This is my Golang code:



 func scoresPage(res http.ResponseWriter, req *http.Request) {

//Connecting to SwaggerUI API to get Scores from Azure for UWP Application

req, err := http.NewRequest("GET", os.ExpandEnv("https://brainworksappservice.azurewebsites.net/tables/TodoItem?$select=score"), nil)
if err != nil {
    log.Fatal(err)
}
//You have to specify these headers
req.Header.Set("Accept", "application/json")
//If you do not specify what version your API is, you cannot receive the JSON
req.Header.Set("Zumo-Api-Version", "2.0.0")

//Do the request
resp, err := http.DefaultClient.Do(req)
//Error if the request cannot be done
if err != nil {
    log.Fatal(err)
}

//You need to close the Body everytime, as if you don't you could leak information
defer resp.Body.Close()

//Read all of the information from the body
body, err := ioutil.ReadAll(resp.Body)

//Error if the info cannot be read
if err != nil {
    log.Fatal(err)
}

//Write the JSON to the standard output (the Console)
_, err = os.Stdout.Write(body)
//Error if the info cannot be output to the console
if err != nil {
    log.Fatal(err)
}

http.ServeFile(res, req, "Scores.html")
}

func main() { http.HandleFunc("/scores", scoresPage)

//serve on the port 8000 forever
http.ListenAndServe(":8000", nil)
}


What I have tried:

I've tried using various Javascript methods but none have worked so far.
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900