"add parser multipart/form-data"

This commit is contained in:
抒情熊 2024-07-24 15:31:43 +08:00
parent 254b9777c0
commit 16e9a14a7d
2 changed files with 18 additions and 5 deletions

View File

@ -24,15 +24,28 @@ func GetRequestBody(c *gin.Context) ([]byte, error) {
return requestBody.([]byte), nil
}
type ModelRequest struct {
Model string `json:"model" form:"model"`
}
func UnmarshalBodyReusable(c *gin.Context, v any) error {
requestBody, err := GetRequestBody(c)
var requestBody []byte
var err error
contentType := c.Request.Header.Get("Content-Type")
if strings.HasPrefix(contentType, "application/json") {
requestBody, err = GetRequestBody(c)
if err != nil {
return err
}
contentType := c.Request.Header.Get("Content-Type")
if strings.HasPrefix(contentType, "application/json") {
err = json.Unmarshal(requestBody, &v)
}
if strings.HasPrefix(contentType, "multipart/form-data") {
var model ModelRequest
err = c.Bind(&model)
requestBody, err = json.Marshal(model)
err = json.Unmarshal(requestBody, &v)
} else {
// skip for now
// TODO: someday non json request have variant model, we will need to implementation this
}

View File

@ -12,7 +12,7 @@ import (
)
type ModelRequest struct {
Model string `json:"model"`
Model string `json:"model" form:"model"`
}
func Distribute() func(c *gin.Context) {