iris/_examples/request-body/read-msgpack/main.go
Gerasimos (Makis) Maropoulos ed45c77be5 reorganization of _examples and add some new examples such as iris+groupcache+mysql+docker
Former-commit-id: ed635ee95de7160cde11eaabc0c1dcb0e460a620
2020-06-07 15:26:06 +03:00

38 lines
903 B
Go

package main
import "github.com/kataras/iris/v12"
// User example struct to bind to.
type User struct {
Firstname string `msgpack:"firstname"`
Lastname string `msgpack:"lastname"`
City string `msgpack:"city"`
Age int `msgpack:"age"`
}
// readMsgPack reads a `User` from MsgPack post body.
func readMsgPack(ctx iris.Context) {
var u User
err := ctx.ReadMsgPack(&u)
if err != nil {
ctx.StopWithError(iris.StatusBadRequest, err)
return
}
ctx.Writef("Received: %#+v\n", u)
}
func main() {
app := iris.New()
app.Post("/", readMsgPack)
// POST: http://localhost:8080
//
// To run the example, use a tool like Postman:
// 1. Body: Binary
// 2. Select File, select the one from "_examples/response-writer/write-rest" example.
// The output should be:
// Received: main.User{Firstname:"John", Lastname:"Doe", City:"Neither FBI knows!!!", Age:25}
app.Listen(":8080")
}