go-zero
go-zero copied to clipboard
Should I call r.Body.Close() when I read data from the request?
trafficstars
In the Go-zero handler model, I didn't use httpx.Parse to parse my request; instead, I parsed it as shown below:
data, err := io.ReadAll(r.Body)
if err != nil {
return err
}
// 解析参数
values, err := url.ParseQuery(string(data))
if err != nil {
return err
}
I noticed that the code generated by goctl parses the request using httpx.Parse, but it doesn't call r.Body.Close().
So, if I use data, err := io.ReadAll(r.Body) to read data and parse my request without using httpx.Parse, should I call r.Body.Close()?
No, you don't need to do it on your own.
net/http handler will do it after the handler finishes.
This issue is stale because it has been open for 30 days with no activity.