From 489334d7eb31198ef8c0def0364b07b478d040c5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=8A=92=E6=83=85=E7=86=8A?= <2669184984@qq.com> Date: Sun, 22 Sep 2024 17:32:47 +0800 Subject: [PATCH] feat: support multipart/form-data format request (#1690) * "add parser multipart/form-data" * chore: fix impl * chore: update impl --------- Co-authored-by: JustSong --- common/gin.go | 6 +++--- middleware/distributor.go | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/common/gin.go b/common/gin.go index 549d3279c9..815b4ee54a 100644 --- a/common/gin.go +++ b/common/gin.go @@ -31,15 +31,15 @@ func UnmarshalBodyReusable(c *gin.Context, v any) error { contentType := c.Request.Header.Get("Content-Type") if strings.HasPrefix(contentType, "application/json") { err = json.Unmarshal(requestBody, &v) + c.Request.Body = io.NopCloser(bytes.NewBuffer(requestBody)) } else { - // skip for now - // TODO: someday non json request have variant model, we will need to implementation this + c.Request.Body = io.NopCloser(bytes.NewBuffer(requestBody)) + err = c.ShouldBind(&v) } if err != nil { return err } // Reset request body - c.Request.Body = io.NopCloser(bytes.NewBuffer(requestBody)) return nil } diff --git a/middleware/distributor.go b/middleware/distributor.go index 0c4b04c341..e2f7511075 100644 --- a/middleware/distributor.go +++ b/middleware/distributor.go @@ -12,7 +12,7 @@ import ( ) type ModelRequest struct { - Model string `json:"model"` + Model string `json:"model" form:"model"` } func Distribute() func(c *gin.Context) {