mirror of
https://github.com/gin-gonic/gin.git
synced 2026-06-07 04:38:19 +08:00
quick fix to skip binding body without content type
This commit is contained in:
parent
e00c839047
commit
71e83183db
@ -36,11 +36,16 @@ func (allBinding) BindMany(req *http.Request, uriParams map[string][]string, obj
|
|||||||
|
|
||||||
// from context.Bind (for body/post-form/anything else)
|
// from context.Bind (for body/post-form/anything else)
|
||||||
contentType := req.Header.Get("Content-Type")
|
contentType := req.Header.Get("Content-Type")
|
||||||
// trim contentType parameters, e.g. "application/json; charset=utf-8" -> "application/json"
|
contentTypeLastIdx := strings.IndexAny(contentType, " ;") // trim "application/json; charset=utf-8" -> "application/json"
|
||||||
contentTypeLastIdx := strings.IndexAny(contentType, " ;")
|
|
||||||
if contentTypeLastIdx != -1 {
|
if contentTypeLastIdx != -1 {
|
||||||
contentType = contentType[:contentTypeLastIdx]
|
contentType = contentType[:contentTypeLastIdx]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// if no Content-Type, assume request has no body. This avoids binding query params again in binding.Default
|
||||||
|
if contentType == "" {
|
||||||
|
return validate(obj)
|
||||||
|
}
|
||||||
|
|
||||||
b := Default(req.Method, contentType)
|
b := Default(req.Method, contentType)
|
||||||
// final validation done by whatever binding is selected here
|
// final validation done by whatever binding is selected here
|
||||||
return b.Bind(req, obj)
|
return b.Bind(req, obj)
|
||||||
|
|||||||
@ -810,7 +810,8 @@ func (c *Context) BindUri(obj any) error {
|
|||||||
// Note:
|
// Note:
|
||||||
// - Caller must tag struct fields appropriately for the desired binding (eg `header:"xxx"` vs `uri:"xxx"`)
|
// - Caller must tag struct fields appropriately for the desired binding (eg `header:"xxx"` vs `uri:"xxx"`)
|
||||||
// - Caller must ensure no duplication between field names (else use separate binding engines instead)
|
// - Caller must ensure no duplication between field names (else use separate binding engines instead)
|
||||||
// - Caller must provide Content-Type header to select the correct body binding (eg "application/json" for JSON binding)
|
// - Caller must provide Content-Type header to select the correct body binding (eg "application/json" for JSON binding).
|
||||||
|
// A request without Content-Type will result in no body binding
|
||||||
// - Binding validation tags are verified after all request parts have been bound
|
// - Binding validation tags are verified after all request parts have been bound
|
||||||
func (c *Context) BindAll(obj any) error {
|
func (c *Context) BindAll(obj any) error {
|
||||||
err := c.ShouldBindAll(obj)
|
err := c.ShouldBindAll(obj)
|
||||||
@ -947,7 +948,8 @@ func (c *Context) ShouldBindUri(obj any) error {
|
|||||||
// Note:
|
// Note:
|
||||||
// - Caller must tag struct fields appropriately for the desired binding (eg `header:"xxx"` vs `uri:"xxx"`)
|
// - Caller must tag struct fields appropriately for the desired binding (eg `header:"xxx"` vs `uri:"xxx"`)
|
||||||
// - Caller must ensure no duplication between field names (else use separate binding engines instead)
|
// - Caller must ensure no duplication between field names (else use separate binding engines instead)
|
||||||
// - Caller must provide Content-Type header to select the correct body binding (eg "application/json" for JSON binding)
|
// - Caller must provide Content-Type header to select the correct body binding (eg "application/json" for JSON binding).
|
||||||
|
// A request without Content-Type will result in no body binding
|
||||||
// - Binding validation tags are verified after all request parts have been bound
|
// - Binding validation tags are verified after all request parts have been bound
|
||||||
func (c *Context) ShouldBindAll(obj any) error {
|
func (c *Context) ShouldBindAll(obj any) error {
|
||||||
uriParams := make(map[string][]string, len(c.Params))
|
uriParams := make(map[string][]string, len(c.Params))
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user