From f640a4972ccec5b33245f62926416067c70bd159 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 12:11:14 +0800 Subject: [PATCH] handle Body return EOF handle Body will return EOF when it used in middleware. --- binding/json.go | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/binding/json.go b/binding/json.go index f968161b..9ad0aa0b 100644 --- a/binding/json.go +++ b/binding/json.go @@ -28,6 +28,13 @@ func (jsonBinding) Bind(req *http.Request, obj interface{}) error { if req == nil || req.Body == nil { return fmt.Errorf("invalid request") } + // read the body to a variable + bodyBytes, _ := ioutil.ReadAll(req.Body) + req.Body.Close() + tempBody := ioutil.NopCloser(bytes.NewBuffer(bodyBytes)) + + //reset the body to the original unread state + req.Body = ioutil.NopCloser(bytes.NewBuffer(bodyBytes)) return decodeJSON(req.Body, obj) }