-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
42 lines (34 loc) · 945 Bytes
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package main
import (
"encoding/json"
"fmt"
"log"
"net/http"
)
type User struct {
FirstName string `json:"firstname"`
LastName string `json:"lastname"`
Age int `json:"age"`
}
func main() {
// test: curl --json "{\"firstname\":\"Yuuki\",\"lastname\":\"NguyenNgocMinhQuocccccc\",\"age\":24}" http://localhost:8088/decode
http.HandleFunc("/decode", func(w http.ResponseWriter, r *http.Request) {
var user User
err := json.NewDecoder(r.Body).Decode(&user)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
fmt.Fprintf(w, "%s %s is %d years old.", user.FirstName, user.LastName, user.Age)
})
// curl -s http://localhost:8088/encode
http.HandleFunc("/encode", func(w http.ResponseWriter, r *http.Request) {
yuuki := User{
FirstName: "Yuuki",
LastName: "Kaitou",
Age: 00,
}
json.NewEncoder(w).Encode(yuuki)
})
log.Fatal(http.ListenAndServe(":8088", nil))
}