支持gin请求表单解析
This commit is contained in:
55
request/form.go
Normal file
55
request/form.go
Normal file
@ -0,0 +1,55 @@
|
||||
// Package request ...
|
||||
//
|
||||
// Description : request ...
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 2022-07-03 00:33
|
||||
package request
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
// form 表单相关操作
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 00:51 2022/7/3
|
||||
type form struct {
|
||||
}
|
||||
|
||||
// ParseRequestForm 解析请求表单
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 00:34 2022/7/3
|
||||
func (f *form) ParseRequestForm(ctx *gin.Context, receiver interface{}) error {
|
||||
method := strings.ToUpper(ctx.Request.Method)
|
||||
if method == http.MethodGet ||
|
||||
method == http.MethodPatch ||
|
||||
method == http.MethodTrace ||
|
||||
method == http.MethodConnect ||
|
||||
method == http.MethodOptions {
|
||||
return ctx.ShouldBindQuery(receiver)
|
||||
}
|
||||
|
||||
if method == http.MethodPost ||
|
||||
method == http.MethodPut ||
|
||||
method == http.MethodDelete {
|
||||
if ContentType.IsJson(ctx) {
|
||||
return ctx.ShouldBindJSON(receiver)
|
||||
}
|
||||
|
||||
if ContentType.IsFormURLEncoded(ctx) {
|
||||
return ctx.ShouldBind(receiver)
|
||||
}
|
||||
|
||||
return errors.New(ctx.ContentType() + " is not support")
|
||||
}
|
||||
return errors.New(method + " : request method is not support")
|
||||
}
|
Reference in New Issue
Block a user