mirror of
https://github.com/kataras/iris.git
synced 2025-12-19 19:07:06 +00:00
add context.Protobuf, MsgPack, ReadProtobuf, ReadMsgPack methods
Former-commit-id: 39d547ecfb1516505a1eb76a12a1f6e9e4111962
This commit is contained in:
38
_examples/http_request/read-msgpack/main.go
Normal file
38
_examples/http_request/read-msgpack/main.go
Normal file
@@ -0,0 +1,38 @@
|
||||
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.StatusCode(iris.StatusBadRequest)
|
||||
ctx.WriteString(err.Error())
|
||||
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/http_responsewriter/write-rest" example.
|
||||
// The output should be:
|
||||
// Received: main.User{Firstname:"John", Lastname:"Doe", City:"Neither FBI knows!!!", Age:25}
|
||||
app.Listen(":8080")
|
||||
}
|
||||
Reference in New Issue
Block a user