当前位置: 首页 > 工具软件 > Go-Request > 使用案例 >

golang http request 读两次body

邬良才
2023-12-01

方法是body重新赋值,

bodyBytes, _ := ioutil.ReadAll(req.Body)
req.Body.Close()  //  must close
req.Body = ioutil.NopCloser(bytes.NewBuffer(bodyBytes))

看了三个网页,都是这样做的:
https://stackoverflow.com/questions/46948050/how-to-read-response-body-twice-in-golang-middleware
https://medium.com/@xoen/golang-read-from-an-io-readwriter-without-loosing-its-content-2c6911805361
http://blog.manugarri.com/how-to-reuse-http-response-body-in-golang/

 类似资料: