"add parser multipart/form-data"
This commit is contained in:
parent
254b9777c0
commit
16e9a14a7d
@ -24,15 +24,28 @@ 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 {
|
||||||
requestBody, err := GetRequestBody(c)
|
var requestBody []byte
|
||||||
if err != nil {
|
var err error
|
||||||
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 {
|
||||||
|
|
||||||
// skip for now
|
// skip for now
|
||||||
// TODO: someday non json request have variant model, we will need to implementation this
|
// TODO: someday non json request have variant model, we will need to implementation this
|
||||||
}
|
}
|
||||||
|
@ -12,7 +12,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type ModelRequest struct {
|
type ModelRequest struct {
|
||||||
Model string `json:"model"`
|
Model string `json:"model" form:"model"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func Distribute() func(c *gin.Context) {
|
func Distribute() func(c *gin.Context) {
|
||||||
|
Loading…
Reference in New Issue
Block a user