From e35d1f17e7a4bf44a96fa52438a61310e6d47f7e Mon Sep 17 00:00:00 2001 From: pham vinh dat Date: Fri, 22 Nov 2024 14:44:19 +0700 Subject: [PATCH 1/2] fix(bind body): content-length can be -1 --- bind.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bind.go b/bind.go index ed7ca3249..acc346506 100644 --- a/bind.go +++ b/bind.go @@ -67,7 +67,7 @@ func (b *DefaultBinder) BindQueryParams(c Context, i interface{}) error { // See MIMEMultipartForm: https://golang.org/pkg/net/http/#Request.ParseMultipartForm func (b *DefaultBinder) BindBody(c Context, i interface{}) (err error) { req := c.Request() - if req.ContentLength == 0 { + if req.ContentLength <= 0 { return } From abb65844f371467f1d8d31c4ad67b57576fbc673 Mon Sep 17 00:00:00 2001 From: pham vinh dat Date: Fri, 22 Nov 2024 15:22:47 +0700 Subject: [PATCH 2/2] add unit-test --- bind_test.go | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/bind_test.go b/bind_test.go index c79669c8c..a7e8dbb3a 100644 --- a/bind_test.go +++ b/bind_test.go @@ -1060,6 +1060,14 @@ func TestDefaultBinder_BindBody(t *testing.T) { expect: &Node{ID: 0, Node: ""}, expectError: "code=415, message=Unsupported Media Type", }, + { + name: "ok, JSON POST bind to struct with: path + query + http.NoBody", + givenURL: "/api/real_node/endpoint?node=xxx", + givenMethod: http.MethodPost, + givenContentType: MIMEApplicationJSON, + givenContent: http.NoBody, + expect: &Node{ID: 0, Node: ""}, + }, } for _, tc := range testCases {