When I use the GoLand tool for debugging,After using HttpClient to request an address in Middleware, the status of the original r *http.Request.Body becomes closed, resulting in an exception during parsing in the Handler: error: http: invalid Read on closed Body.
Middleware:
func (m *AuthorizeHandlerMiddleware) Handle(next http.HandlerFunc) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
token := r.Header.Get("Authorization")
if token == "" {
res := types.Base{
Code: 401,
Message: "error",
}
logc.Error(r.Context(), "token is empty")
httpx.OkJsonCtx(r.Context(), w, res)
return
}
hReq, err := http.NewRequest("GET", "http://localhost:801/admin/member/info", nil)
if err != nil {
logc.Error(r.Context(), err)
return
}
c := http.Client{}
_, _ = c.Do(hReq) //After executing this step, r.Body's Close becomes True.
next(w, r)
}
}
Handler
func MyHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var req types.MyReq
if err := httpx.Parse(r, &req); err != nil {
// error : http: invalid Read on closed Body.
httpx.ErrorCtx(r.Context(), w, err)
return
}
l := logic.NewAnaliysisPnrLogic(r.Context(), svcCtx)
resp, err := l.AnaliysisPnr(&req)
if err != nil {
httpx.ErrorCtx(r.Context(), w, err)
} else {
httpx.OkJsonCtx(r.Context(), w, resp)
}
}
}
Please help me check the reason for the problem. Thank you.