mirror of
https://github.com/rjNemo/go-wiki
synced 2026-06-12 13:36:38 +00:00
fetch
This commit is contained in:
parent
e3e6ad958e
commit
a2a3022b2a
2 changed files with 42 additions and 1 deletions
42
data/fetch.go
Normal file
42
data/fetch.go
Normal 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
|
||||||
|
}
|
||||||
1
main.go
1
main.go
|
|
@ -8,7 +8,6 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
|
||||||
fmt.Printf("Start Go-wiki server on http://localhost:%s at %s\n", controller.Port, time.Now())
|
fmt.Printf("Start Go-wiki server on http://localhost:%s at %s\n", controller.Port, time.Now())
|
||||||
controller.RegisteredRoutes()
|
controller.RegisteredRoutes()
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue