Skip to content

Commit

Permalink
chore: fix impl
Browse files Browse the repository at this point in the history
  • Loading branch information
songquanpeng committed Sep 22, 2024
1 parent 16e9a14 commit a159fa9
Showing 1 changed file with 5 additions and 19 deletions.
24 changes: 5 additions & 19 deletions common/gin.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,30 +24,16 @@ 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 {
var requestBody []byte
var err error
requestBody, err := GetRequestBody(c)
if err != nil {
return err
}
contentType := c.Request.Header.Get("Content-Type")
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)
} else {

// skip for now
// TODO: someday non json request have variant model, we will need to implementation this
err = c.ShouldBind(&v)
}
if err != nil {
return err
Expand Down

0 comments on commit a159fa9

Please sign in to comment.