Request-response utility for every HTTP context
mux.Post("/", func(rw http.ResponseWriter, r *http.Request) error {
c := ctx.New(rw, r)
var data interface{}
if err := c.GetJSON(&data); err != nil {
return c.JSON(http.StatusBadRequest, ngamux.Map{
"error": err.Error(),
})
}
return c.JSON(http.StatusOK, data)
})
or use handler adapter
mux.Post("/", ctx.Handler(func(c *ctx.Context) error {
var data interface{}
if err := c.GetJSON(&data); err != nil {
return c.JSON(http.StatusBadRequest, ngamux.Map{
"error": err.Error(),
})
}
return c.JSON(http.StatusOK, data)
}))
or use mux adapter
mux := ctx.Mux(ngamux.New())
mux.Post("/", func(c *ctx.Context) error {
var data interface{}
if err := c.GetJSON(&data); err != nil {
return c.JSON(http.StatusBadRequest, ngamux.Map{
"error": err.Error(),
})
}
return c.JSON(http.StatusOK, data)
})