iris/_examples/http_request/read-json/main.go
kataras 42b123975c 8.4.0 | New MVC Features | Refactor examples and godoc for go 1.9 use. Read HISTORY.md.
Former-commit-id: 90c05e743052bc3722e7fefaa0cbb0ed5153a1fb
2017-08-27 20:35:23 +03:00

44 lines
771 B
Go

package main
import (
"github.com/kataras/iris"
)
type Company struct {
Name string
City string
Other string
}
func MyHandler(ctx iris.Context) {
c := &Company{}
if err := ctx.ReadJSON(c); err != nil {
ctx.StatusCode(iris.StatusBadRequest)
ctx.WriteString(err.Error())
return
}
ctx.Writef("Received: %#v\n", c)
}
func main() {
app := iris.New()
app.Post("/", MyHandler)
// 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
//
// The response should be:
// Received: &main.Company{Name:"iris-Go", City:"New York", Other:"Something here"}
app.Run(iris.Addr(":8080"))
}