iris/_examples/http_request/read-json/main.go
2017-07-10 18:32:42 +03:00

45 lines
809 B
Go

package main
import (
"github.com/kataras/iris"
"github.com/kataras/iris/context"
)
type Company struct {
Name string
City string
Other string
}
func MyHandler(ctx context.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"))
}