修复表单解析panic问题

This commit is contained in:
白茶清欢 2023-02-12 22:26:03 +08:00
parent 7678ee10ba
commit 77d2310b27

View File

@ -36,21 +36,6 @@ type form struct {
// //
// Date : 00:34 2022/7/3 // Date : 00:34 2022/7/3
func (f *form) Parse(ctx *gin.Context, receiver interface{}) error { func (f *form) Parse(ctx *gin.Context, receiver interface{}) error {
var validateFunc = func(parseErr error) error {
if nil != parseErr {
return parseErr
}
receiverIsStruct := false
switch receiver.(type) {
case struct{}:
receiverIsStruct = true
}
if !receiverIsStruct {
// 不是结构体,不验证表单
return nil
}
return f.checkInstance.Struct(receiver)
}
requestBody, _ := ioutil.ReadAll(ctx.Request.Body) requestBody, _ := ioutil.ReadAll(ctx.Request.Body)
// 请求信息写入上下文 // 请求信息写入上下文
@ -63,18 +48,18 @@ func (f *form) Parse(ctx *gin.Context, receiver interface{}) error {
method == http.MethodTrace || method == http.MethodTrace ||
method == http.MethodConnect || method == http.MethodConnect ||
method == http.MethodOptions { method == http.MethodOptions {
return validateFunc(ctx.ShouldBindQuery(receiver)) return ctx.ShouldBindQuery(receiver)
} }
if method == http.MethodPost || if method == http.MethodPost ||
method == http.MethodPut || method == http.MethodPut ||
method == http.MethodDelete { method == http.MethodDelete {
if ContentType.IsJson(ctx) { if ContentType.IsJson(ctx) {
return validateFunc(ctx.ShouldBindJSON(receiver)) return ctx.ShouldBindJSON(receiver)
} }
if ContentType.IsFormURLEncoded(ctx) { if ContentType.IsFormURLEncoded(ctx) {
return validateFunc(ctx.ShouldBind(receiver)) return ctx.ShouldBind(receiver)
} }
return errors.New(ctx.ContentType() + " is not support") return errors.New(ctx.ContentType() + " is not support")