-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
/
main.go
62 lines (51 loc) · 1.23 KB
/
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package main
import (
"github.com/kataras/iris/v12"
)
type Company struct {
Name string
City string
Other string
}
func MyHandler(ctx iris.Context) {
var c Company
if err := ctx.ReadJSON(&c); err != nil {
ctx.StopWithError(iris.StatusBadRequest, err)
return
}
ctx.Writef("Received: %#+v\n", c)
}
// simple json stuff, read more at https://golang.org/pkg/encoding/json
type Person struct {
Name string `json:"name"`
Age int `json:"age"`
}
// MyHandler2 reads a collection of Person from JSON post body.
func MyHandler2(ctx iris.Context) {
var persons []Person
err := ctx.ReadJSON(&persons)
if err != nil {
ctx.StopWithError(iris.StatusBadRequest, err)
return
}
ctx.Writef("Received: %#+v\n", persons)
}
func main() {
app := iris.New()
app.Post("/", MyHandler)
app.Post("/slice", MyHandler2)
// use Postman or whatever to do a POST request
// to the http://localhost:8080 with RAW BODY:
/*
{
"Name": "iris-Go",
"City": "New York",
"Other": "Something here"
}
*/
// and Content-Type to application/json (optionally but good practise)
//
// The response should be:
// Received: main.Company{Name:"iris-Go", City:"New York", Other:"Something here"}
app.Listen(":8080", iris.WithOptimizations)
}