From af0276fe98b2fe4aebf1a13fb9110fd82f26c7e6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9E=97?= <7580755+disilin@users.noreply.github.com> Date: Wed, 13 Mar 2019 13:16:03 +0800 Subject: [PATCH] handle Body return EOF handle Body will return EOF when it used in middleware. --- context.go | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/context.go b/context.go index 5dc7f8a0..b1fa51e6 100644 --- a/context.go +++ b/context.go @@ -5,6 +5,7 @@ package gin import ( + "bytes" "errors" "fmt" "io" @@ -640,6 +641,10 @@ func (c *Context) ShouldBindBodyWith(obj interface{}, bb binding.BindingBody) (e if err != nil { return err } + // close the body + c.Request.Body.Close() + // reset the body to the original unread state + c.Request.Body = ioutil.NopCloser(bytes.NewBuffer(body)) c.Set(BodyBytesKey, body) } return bb.BindBody(body, obj)