Skip to content
Snippets Groups Projects
Commit f8134408 authored by Aksel Baardsen's avatar Aksel Baardsen
Browse files

testing countries api and json parsing, not working

parent 6473a20a
No related branches found
No related tags found
No related merge requests found
package main
import (
"assignment-1/models"
"encoding/json"
"fmt"
"log"
"net/http"
......@@ -34,7 +36,25 @@ func dHandler(w http.ResponseWriter, r *http.Request) {
}
func cHandler(w http.ResponseWriter, r *http.Request) {
_, _ = fmt.Fprintln(w, "aiaiai")
vars := mux.Vars(r)
_, _ = fmt.Fprintln(w, vars["country_identifier"])
resp, _ := http.Get("http://restcountries.eu/rest/v2/alpha/" + vars["country_identifier"] + "/")
cunt1 := &models.Country{}
err := json.NewDecoder(resp.Body).Decode(cunt1)
if err != nil {
fmt.Print("on it")
}
fmt.Fprintln(w, cunt1.CountryName)
qq, _ := json.Marshal(resp.Body)
fmt.Fprintln(w, string(qq))
//country := models.GetCountryByCode(vars["country_identifier"])
//_, _ = fmt.Fprintln(w, country)
}
......
package models
type country struct {
const codeAPI = "https://restcountries.eu/rest/v2/alpha/"
type Country struct {
Code string `json:"code"`
CountryName string `json:"countryname"`
CountryFlag string `json:"countryflag"`
Species []specie `json:"species"`
//Species []Specie `json:"species"`
SpeciesKey []int `json:"specieskey"`
}
/*
func GetCountryByCode(code string) Country {
if err != nil {
//handle
}
c := &Country{}
err = json.NewDecoder(resp.Body).Decode(c)
if err != nil {
//handle
}
defer resp.Body.Close()
return *c
}*/
func (c *Country) show() {
}
\ No newline at end of file
package models
type specie struct {
type Specie struct {
Key int `json:"key"`
Kingdom string `json:"kingdom"`
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment