-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontext.go
More file actions
62 lines (54 loc) · 1.37 KB
/
context.go
File metadata and controls
62 lines (54 loc) · 1.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package web
import (
"encoding/json"
"errors"
"net/http"
"net/url"
)
type Context struct {
Req *http.Request
Resp http.ResponseWriter
Param *param
urlQueries url.Values // Cache the url queries
}
// BindJSON fills val with JSON data
func (c *Context) BindJSON(val any) error {
if c.Req.Body == nil {
return errors.New("body is nil")
}
if val == nil {
return errors.New("val is nil")
}
decoder := json.NewDecoder(c.Req.Body)
//decoder.DisallowUnknownFields() // do not allow unknown fields in JSON
//decoder.UseNumber() // Use `Number`(string) as the type of numbers
return decoder.Decode(val)
}
// FormValue gets value of `key` in form data
func (c *Context) FormValue(key string) (string, error) {
// parsing multiple times is ok
err := c.Req.ParseForm()
if err != nil {
return "", err
}
// c.Req.Form = params in POST, PUT, PATCH and URL
// c.Req.PostForm = params in POST, PUT, PATCH body
val := c.Req.FormValue(key)
return val, nil
}
/*
func (c *Context) BindForm(val any) error {
}
*/
// QueryValue gets a query param `key`
func (c *Context) QueryValue(key string) string {
if c.urlQueries == nil {
c.urlQueries = c.Req.URL.Query()
}
// if not present, return empty string is fine
return c.urlQueries.Get(key)
}
// PathValue gets the value of path param `key`
func (c *Context) PathValue(key string) string {
return (*c.Param)[key]
}