chore: fix impl

This commit is contained in:
JustSong 2024-09-22 17:15:12 +08:00
parent 16e9a14a7d
commit a159fa9832

View File

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