networking and shit
This commit is contained in:
@@ -0,0 +1,46 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"os"
|
||||
)
|
||||
|
||||
const url = "https://jsonplaceholder.typicode.com"
|
||||
|
||||
type todo struct {
|
||||
ID int `json:id`
|
||||
Title string `json:title`
|
||||
Status bool `json: status`
|
||||
}
|
||||
|
||||
func handler(w http.ResponseWriter, r *http.Request) {
|
||||
const base = "https://jsonplaceholder."
|
||||
}
|
||||
|
||||
func main() {
|
||||
resp, err := http.Get(url + "/todos/1/")
|
||||
if err != nil {
|
||||
fmt.Fprintln(os.Stderr, err)
|
||||
os.Exit(-1)
|
||||
}
|
||||
|
||||
defer resp.Body.Close()
|
||||
|
||||
if resp.StatusCode == http.StatusOK {
|
||||
body, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
fmt.Fprintln(os.Stderr, err)
|
||||
os.Exit(-1)
|
||||
}
|
||||
var item todo
|
||||
err = json.Unmarshal(body, &item)
|
||||
if err != nil {
|
||||
fmt.Fprintln(os.Stderr, err)
|
||||
os.Exit(-1)
|
||||
}
|
||||
fmt.Printf("%#v", item)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user