Skip to content
/ ctx Public

Request-response utility for every HTTP context

Notifications You must be signed in to change notification settings

ngamux/ctx

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

20 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

ctx

Request-response utility for every HTTP context

Usage

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)
})

About

Request-response utility for every HTTP context

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages