From 71e83183db777b32d75adda56da8a5e6b18d81e9 Mon Sep 17 00:00:00 2001 From: takanuva15 <6986426+takanuva15@users.noreply.github.com> Date: Mon, 30 Mar 2026 17:29:21 -0400 Subject: [PATCH] quick fix to skip binding body without content type --- binding/all.go | 9 +++++++-- context.go | 18 ++++++++++-------- 2 files changed, 17 insertions(+), 10 deletions(-) diff --git a/binding/all.go b/binding/all.go index fbaee1e3..dd2a3ad8 100644 --- a/binding/all.go +++ b/binding/all.go @@ -36,11 +36,16 @@ func (allBinding) BindMany(req *http.Request, uriParams map[string][]string, obj // from context.Bind (for body/post-form/anything else) contentType := req.Header.Get("Content-Type") - // trim contentType parameters, e.g. "application/json; charset=utf-8" -> "application/json" - contentTypeLastIdx := strings.IndexAny(contentType, " ;") + contentTypeLastIdx := strings.IndexAny(contentType, " ;") // trim "application/json; charset=utf-8" -> "application/json" if contentTypeLastIdx != -1 { 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) // final validation done by whatever binding is selected here return b.Bind(req, obj) diff --git a/context.go b/context.go index 7c8509e7..0106504c 100644 --- a/context.go +++ b/context.go @@ -808,10 +808,11 @@ func (c *Context) BindUri(obj any) error { // It will abort the request with HTTP 400 if any error occurs. // // Note: -// - 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 provide Content-Type header to select the correct body binding (eg "application/json" for JSON binding) -// - Binding validation tags are verified after all request parts have been bound +// - 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 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 func (c *Context) BindAll(obj any) error { err := c.ShouldBindAll(obj) if err != nil { @@ -945,10 +946,11 @@ func (c *Context) ShouldBindUri(obj any) error { // Like c.Bind() but this method does not set the response status code to 400 or abort if input is not valid. // // Note: -// - 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 provide Content-Type header to select the correct body binding (eg "application/json" for JSON binding) -// - Binding validation tags are verified after all request parts have been bound +// - 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 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 func (c *Context) ShouldBindAll(obj any) error { uriParams := make(map[string][]string, len(c.Params)) for _, v := range c.Params {