diff --git a/pkg/app/server/binding/binder_test.go b/pkg/app/server/binding/binder_test.go index 2a66229c7..0414a7a57 100644 --- a/pkg/app/server/binding/binder_test.go +++ b/pkg/app/server/binding/binder_test.go @@ -1556,3 +1556,31 @@ func Benchmark_Binding(b *testing.B) { } } } + +// TestBind_BindProtobufWithoutContentLength for issue-983 +func TestBind_BindProtobufWithoutContentLength(t *testing.T) { + data := testdata.HertzReq{Name: "hertz"} + body, err := proto.Marshal(&data) + if err != nil { + t.Fatal(err) + } + req := newMockRequest(). + SetRequestURI("http://foobar.com"). + SetProtobufContentType(). + SetBody(body) + + req.Req.Header.Del("Content-Length") + result := testdata.HertzReq{} + err = DefaultBinder().BindAndValidate(req.Req, &result, nil) + if err != nil { + t.Error(err) + } + assert.DeepEqual(t, "hertz", result.Name) + + result = testdata.HertzReq{} + err = DefaultBinder().BindProtobuf(req.Req, &result) + if err != nil { + t.Error(err) + } + assert.DeepEqual(t, "hertz", result.Name) +} diff --git a/pkg/app/server/binding/default.go b/pkg/app/server/binding/default.go index 0634f26cf..2aaa7ef52 100644 --- a/pkg/app/server/binding/default.go +++ b/pkg/app/server/binding/default.go @@ -319,7 +319,7 @@ func (b *defaultBinder) Bind(req *protocol.Request, v interface{}, params param. // best effort binding func (b *defaultBinder) preBindBody(req *protocol.Request, v interface{}) error { - if req.Header.ContentLength() <= 0 { + if len(req.Body()) == 0 { return nil } ct := bytesconv.B2s(req.Header.ContentType())