I want to make middleware func to check token's validity, but at the end function I cannot get post value of it's request.
Here I put "brief" (code just to show the main idea)
func main() {
http.Handle("/100000/000001", Verify(http.HandlerFunc(Insert)))
}
func Verify(next http.Handler) http.Handler
{
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
var reqBody RequestBody
decoder := json.NewDecoder(r.Body)
err := decoder.Decode(&reqBody)
Util.CheckErr(err)
var token = RequestHeader{Token: r.Header.Get("_Token")}
if token == dbToken {
next.ServeHTTP(w, r)
} else {
Util.PutResponseMessage(w, "003", "Access danied")
}
})
}
func Insert(res http.ResponseWriter, req *http.Request) {
var requestBody InsertRequestBody
decoder := json.NewDecoder(req.Body)
err := decoder.Decode(&requestBody)
if err != nil {
panic(err)
}
defer req.Body.Close()
log.Println(requestBody.Username)
}
I get error like this :
http: panic serving [::1]:51269: EOF
goroutine 6 [running]:
net/http.(*conn).serve.func1(0xc0420348c0)
C:/Go/src/net/http/server.go:1721 +0xd7
panic(0x6884e0, 0xc042008110)
But, If I directly return next.ServeHttp(r, w)
like this :
func Verify(next http.Handler) http.Handler
{
next.ServeHTTP(w, r)
}
Then decoding process in Insert func working perfectly.
Why is it? Any answer will highly appreciated. Thankyou