This commit is contained in:
Ruidy Nemausat 2020-03-14 21:23:01 +01:00
parent e3e6ad958e
commit a2a3022b2a
2 changed files with 42 additions and 1 deletions

42
data/fetch.go Normal file
View file

@ -0,0 +1,42 @@
package data
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"time"
)
var exJson string = "https://jsonplaceholder.typicode.com/todos/1"
func Main() {
b := fetchApi(exJson)
fmt.Println(b)
}
func FetchApi(s string) Data {
r, err := http.NewRequest(http.MethodGet, s, nil)
if err != nil {
log.Fatal(err)
}
Client := http.Client{
Timeout: time.Second * 2, // Maximum of 2 secs
}
rs, err := Client.Do(r)
b, err := ioutil.ReadAll(rs.Body)
d := Data{}
err = json.Unmarshal(b, &d)
if err != nil {
log.Fatal(err)
}
return d
}
type Data struct {
UserId int
Id int
Title string
Completed bool
}

View file

@ -8,7 +8,6 @@ import (
)
func main() {
fmt.Printf("Start Go-wiki server on http://localhost:%s at %s\n", controller.Port, time.Now())
controller.RegisteredRoutes()
}