Bind Any

Bind request body to “ptr” depending on the content-type that client sends the data, e.g. JSON, XML, YAML, MessagePack, Protobuf, Form and URL Query.

  1. package main
  2. import (
  3. "time"
  4. "github.com/kataras/iris/v12"
  5. )
  6. type Person struct {
  7. Name string `form:"name" json:"name" url:"name" msgpack:"name"`
  8. Address string `form:"address" json:"address" url:"address" msgpack:"address"`
  9. Birthday time.Time `form:"birthday" time_format:"2006-01-02" time_utc:"1" json:"birthday" url:"birthday" msgpack:"birthday"`
  10. CreateTime time.Time `form:"createTime" time_format:"unixNano" json:"create_time" url:"create_time" msgpack:"createTime"`
  11. UnixTime time.Time `form:"unixTime" time_format:"unix" json:"unix_time" url:"unix_time" msgpack:"unixTime"`
  12. }
  13. func main() {
  14. app := iris.Default()
  15. app.Any("/", index)
  16. app.Listen(":8080")
  17. }
  18. func index(ctx iris.Context) {
  19. var person Person
  20. if err := ctx.ReadBody(&person); err!=nil {
  21. ctx.StopWithError(iris.StatusBadRequest, err)
  22. return
  23. }
  24. ctx.Application().Logger().Infof("Person: %#+v", person)
  25. ctx.WriteString("Success")
  26. }

Test it with:

  1. $ curl -X GET "localhost:8085/testing?name=kataras&address=xyz&birthday=1992-03-15&createTime=1562400033000000123&unixTime=1562400033"