diff --git a/request/parse_body/execute.go b/request/parse_body/execute.go index 22937e5..bc023c4 100644 --- a/request/parse_body/execute.go +++ b/request/parse_body/execute.go @@ -21,6 +21,7 @@ var ( func init() { adaptorList := []abstract.RequestBodyParseAdaptor{ JsonAdaptor{}, + FormUrlEncode{}, } for _, itemAdaptor := range adaptorList { Register(itemAdaptor) diff --git a/request/parse_body/form_url_encode.go b/request/parse_body/form_url_encode.go index 8a18ca3..a456279 100644 --- a/request/parse_body/form_url_encode.go +++ b/request/parse_body/form_url_encode.go @@ -6,3 +6,39 @@ // // Date : 2024-10-22 16:43 package parse_body + +import ( + "encoding/json" + "git.zhangdeman.cn/zhangdeman/consts" + "git.zhangdeman.cn/zhangdeman/serialize" + "github.com/gin-gonic/gin" +) + +type FormUrlEncode struct { + base +} + +func (f FormUrlEncode) Parse(ctx *gin.Context, receiver any) ([]byte, error) { + if err := ctx.Request.ParseForm(); nil != err { + return nil, err + } + body := map[string]string{} + for paramName, itemParam := range ctx.Request.PostForm { + if len(itemParam) > 0 { + body[paramName] = itemParam[0] + } else { + body[paramName] = "" + } + } + byteData, _ := json.Marshal(body) + if nil != receiver { + if err := serialize.JSON.UnmarshalWithNumber(byteData, receiver); nil != receiver { + return nil, err + } + } + return byteData, nil +} + +func (f FormUrlEncode) ContentType() string { + return consts.MimeTypeXWWWFormUrlencoded +}