diff options
| author | Eltrac <hi@guhub.cn> | 2025-12-03 16:10:50 +0800 |
|---|---|---|
| committer | Eltrac <hi@guhub.cn> | 2025-12-03 16:10:50 +0800 |
| commit | 054f9b22b0f72b703f3f05434a026b280a6692ae (patch) | |
| tree | 3d76af8cf7605fc8774717cf5681e38807c5a7d3 /context.go | |
| parent | a7f5c63a22b8ee35545dbfe2bf8931cb5af8fe23 (diff) | |
feat: provide basic data getters
- c.Query(), from URL query string - c.PostFormValue(), from POST form -
c.FormValue(), from both URL query and POST form - c.BindJSON(), from request
body
Diffstat (limited to 'context.go')
| -rw-r--r-- | context.go | 34 |
1 files changed, 33 insertions, 1 deletions
@@ -80,7 +80,7 @@ func (c *Context) InternalError(msg string) { c.Error(http.StatusInternalServerError, msg) } -/* Cookies */ +/* === Cookies === */ func (c *Context) Cookies() []*http.Cookie { return c.R.Cookies() @@ -111,3 +111,35 @@ func (c *Context) SetCookie(name string, value string) { } c.SetCompleteCookie(&cookie) } + +/* === Request Data === */ + +// Context.FormValue gets value from request form +// form contains URL querys and POST Form value +// +// alias of 'c.Form[key]` +func (c *Context) FormValue(key string) string { + return c.Form.Get(key) +} + +// Context.PostFormValue works similarly to Context.FormValue +// but it does NOT contain URL querys, only POST form +func (c *Context) PostFormValue(key string) string { + return c.R.PostFormValue(key) +} + +// Context.Query gets value from request URL query +// +// alias of 'c.Querys[key]' +func (c *Context) Query(key string) string { + return c.Querys.Get(key) +} + +// Context.BindJSON takes the container for JSON data +// decodes request body and then writes data to container +func (c *Context) BindJSON(v any) error { + c.R.Body = http.MaxBytesReader(c.W, c.R.Body, 2<<20) + dec := json.NewDecoder(c.R.Body) + dec.DisallowUnknownFields() + return dec.Decode(v) +} |
