-
Notifications
You must be signed in to change notification settings - Fork 3
/
request_param.go
101 lines (88 loc) · 2.5 KB
/
request_param.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
package ginKit
import (
"github.com/gin-gonic/gin"
"github.com/richelieu-yang/chimera/v3/src/core/boolKit"
"github.com/richelieu-yang/chimera/v3/src/core/floatKit"
"github.com/richelieu-yang/chimera/v3/src/core/intKit"
"github.com/richelieu-yang/chimera/v3/src/core/strKit"
"io"
)
// ObtainGetParam 获取 GET 请求的参数(从url获取).
/*
PS:
(1) 原生方法的用法更加丰富!
(2) 不需要额外手动解码.
*/
func ObtainGetParam(ctx *gin.Context, key string) string {
return ctx.Query(key)
}
// ObtainPostParam 获取 POST 请求的参数.
/*
PS:
(1) 原生方法的用法更加丰富!
(2) 不需要额外手动解码;
(3) 请求的Content-Type, (a) 支持: multipart/form-data、x-www-form-urlencoded(即application/x-www-form-urlencoded)
(b) 不支持: application/json
*/
func ObtainPostParam(ctx *gin.Context, key string) string {
return ctx.PostForm(key)
}
// ObtainParam 获取请求参数.
/*
PS:
(1) 优先级(从左到右): GET、POST;
(2) 不需要额外手动解码.
*/
func ObtainParam(ctx *gin.Context, key string) string {
// (1) GET
value := ctx.Query(key)
if strKit.IsNotEmpty(value) {
return value
}
// (2) POST
return ctx.PostForm(key)
}
func ObtainBoolParam(ctx *gin.Context, key string) (bool, error) {
value := ObtainParam(ctx, key)
return boolKit.ToBoolE(value)
}
func ObtainIntParam(ctx *gin.Context, key string) (int, error) {
value := ObtainParam(ctx, key)
return intKit.ToIntE(value)
}
func ObtainInt32Param(ctx *gin.Context, key string) (int32, error) {
value := ObtainParam(ctx, key)
return intKit.ToInt32E(value)
}
func ObtainInt64Param(ctx *gin.Context, key string) (int64, error) {
value := ObtainParam(ctx, key)
return intKit.ToInt64E(value)
}
func ObtainFloat32Param(ctx *gin.Context, key string) (float32, error) {
value := ObtainParam(ctx, key)
return floatKit.ToFloat32E(value)
}
func ObtainFloat64Param(ctx *gin.Context, key string) (float64, error) {
value := ObtainParam(ctx, key)
return floatKit.ToFloat64E(value)
}
// ObtainFormFileContent form请求,根据 传参key 获取文件的字节流.(单文件上传)
/*
@return 文件内容 + 文件名 + 错误
*/
func ObtainFormFileContent(ctx *gin.Context, key string) ([]byte, string, error) {
fileHeader, err := ctx.FormFile(key)
if err != nil {
return nil, "", err
}
file, err := fileHeader.Open()
if err != nil {
return nil, "", err
}
defer file.Close()
content, err := io.ReadAll(file)
if err != nil {
return nil, "", err
}
return content, fileHeader.Filename, nil
}