go-zero icon indicating copy to clipboard operation
go-zero copied to clipboard

Should I call r.Body.Close() when I read data from the request?

Open rotbit opened this issue 1 year ago • 1 comments
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()?

rotbit avatar Sep 20 '24 04:09 rotbit

No, you don't need to do it on your own.

net/http handler will do it after the handler finishes.

kevwan avatar Sep 20 '24 14:09 kevwan

This issue is stale because it has been open for 30 days with no activity.

github-actions[bot] avatar Sep 25 '25 02:09 github-actions[bot]