-
Notifications
You must be signed in to change notification settings - Fork 1
/
request.go
55 lines (48 loc) · 1.26 KB
/
request.go
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
package u_handler
import (
"net/http"
"strconv"
"strings"
)
func (h *BaseHandler) RequestParamStr(r *http.Request, key string) string {
return r.URL.Query().Get(key)
}
func (h *BaseHandler) RequestParamStrWithDefault(r *http.Request, key string, defaultValue string) string {
value := r.URL.Query().Get(key)
if value == "" {
return defaultValue
}
return value
}
func (h *BaseHandler) RequestParamInt(r *http.Request, key string, defaultValue int64) int64 {
value := r.URL.Query().Get(key)
valueInt, err := strconv.ParseInt(value, 10, 64)
if err != nil {
return defaultValue
}
return valueInt
}
func (h *BaseHandler) RequestParamFloat(r *http.Request, key string, defaultValue float64) float64 {
value := r.URL.Query().Get(key)
valueInt, err := strconv.ParseFloat(value, 64)
if err != nil {
return defaultValue
}
return valueInt
}
func (h *BaseHandler) RequestParamBool(r *http.Request, key string, defaultValue bool) bool {
value := r.URL.Query().Get(key)
valueBool, err := strconv.ParseBool(value)
if err != nil {
return defaultValue
}
return valueBool
}
func (h *BaseHandler) RequestParamArray(r *http.Request, key string) []string {
value := r.URL.Query().Get(key)
if value == "" {
return []string{}
} else {
return strings.Split(value, ",")
}
}