golang http request 读两次body

前端之家收集整理的这篇文章主要介绍了golang http request 读两次body前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

方法是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/

原文链接:https://www.f2er.com/go/187097.html

猜你在找的Go相关文章