Skip to content

Commit 7cdcd74

Browse files
committed
请求body最大值可以设置
1 parent 5b78dcd commit 7cdcd74

File tree

1 file changed

+14
-12
lines changed

1 file changed

+14
-12
lines changed

http/request/request.go

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,21 @@ import (
99
"github.com/infraboard/mcube/exception"
1010
)
1111

12-
// CheckBody 读取Body当中的数据
13-
func CheckBody(r *http.Request) ([]byte, error) {
12+
var (
13+
// BodyMaxContenxLength body 最大大小 默认 64M
14+
BodyMaxContenxLength int64 = 1 << 26
15+
)
16+
17+
// ReadBody 读取Body当中的数据
18+
func ReadBody(r *http.Request) ([]byte, error) {
1419
// 检测请求大小
1520
if r.ContentLength == 0 {
1621
return nil, exception.NewBadRequest("request body is empty")
1722
}
18-
if r.ContentLength > 20971520 {
23+
if r.ContentLength > BodyMaxContenxLength {
1924
return nil, exception.NewBadRequest(
20-
"the body exceeding the maximum limit, max size 20M")
25+
"the body exceeding the maximum limit, max size %dM",
26+
BodyMaxContenxLength/1024/1024)
2127
}
2228

2329
// 读取body数据
@@ -32,16 +38,12 @@ func CheckBody(r *http.Request) ([]byte, error) {
3238
return body, nil
3339
}
3440

35-
// GetObjFromReq todo
36-
func GetObjFromReq(r *http.Request, v interface{}) error {
37-
body, err := CheckBody(r)
41+
// GetDataFromRequest todo
42+
func GetDataFromRequest(r *http.Request, v interface{}) error {
43+
body, err := ReadBody(r)
3844
if err != nil {
3945
return err
4046
}
4147

42-
if err := json.Unmarshal(body, v); err != nil {
43-
return err
44-
}
45-
46-
return nil
48+
return json.Unmarshal(body, v)
4749
}

0 commit comments

Comments
 (0)