From cffb0c87795cfbe226c8c6c4bd1017fa1b920a2f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=99=BD=E8=8C=B6=E6=B8=85=E6=AC=A2?= Date: Tue, 22 Oct 2024 17:20:30 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0form=20url=20encode=E8=A7=A3?= =?UTF-8?q?=E6=9E=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- request/parse_body/execute.go | 1 + request/parse_body/form_url_encode.go | 36 +++++++++++++++++++++++++++ 2 files changed, 37 insertions(+) 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 +}