ioutils.ReadAll()会清空对应的reader
来源:互联网 发布:淘宝客推广流程 编辑:程序博客网 时间:2024/05/02 04:33
解决办法:
//copy first, or Boby will be emptyreqBytes, err := ioutil.ReadAll(c.Request.Body)if err != nil {log.Error("fail to read requset data")return 0, err}log.Infof("%s", reqBytes)buf := bytes.NewBuffer(reqBytes)c.Request.Body = ioutil.NopCloser(buf)
**********************************************************************************
原因分析:
http://stackoverflow.com/questions/30910487/why-an-io-reader-after-read-it-became-empty-in-golang
Reading from a bytes.Buffer
drains or consumes the bytes that were read. This means if you try to read again those will not be returned.
Buffer.Bytes()
returns the unread portion of the buffer so it is the expected result for you to see 0
length after everything has been read (this is exactly what ioutil.ReadAll()
does).
What if you just want to "peek" and not really "read" bytes?
There is no "peek" functionality in bytes.Buffer
. The easiest would be to get the bytes of the buffer, and construct another bytes.Buffer
from it and read from the new buffer.
It could look something like this:
func peek(buf *bytes.Buffer, b []byte) (int, error) { buf2 := bytes.NewBuffer(buf.Bytes()) return buf2.Read(b)}
实现代码分析: http://www.cnphp6.com/archives/110001
解决思路来源:
Reading body of http.Request without modifying request state?
- ioutils.ReadAll()会清空对应的reader
- ioutils.ReadAll()会清空对应的reader
- Qt QFile的ReadAll函数的注意
- 关于ioutil.ReadAll()的阻塞问题
- IOUtils的简单介绍
- IOUtils的使用
- IOUtils
- IOUtils
- IOUtils
- org.apache.commons.io.IOUtils IOUtils.copy与IOUtils.closeQuietly的用法
- IOUtils 单元(7): TFile 结构的功能
- XE :IOUtils单元TDirectory的其他功能
- Delphi的新工具单元IOUtils
- IOUtils和FileUtils的使用区别
- Reader——Reader的基本概念
- Qt TCP通信readAll()读取接收数据时无法读完大数据量的解决法案
- QProcess::readAll() 函数
- Google的RSS reader
- 谈谈对Spring IOC的理解
- 图像检索----迭代量化(Iterative Quantization)理解
- Hadoop Hive sql语法详解--DQL 操作:数据查询SQL(4)
- Java拓扑图开发-带宽示例
- 前端开发入门:框架篇-ExpressJS入门学习
- ioutils.ReadAll()会清空对应的reader
- 读取外部存储的文件数据
- C语言中#define的用法
- ios设计模式4-桥接模式
- 粒子滤波的MATLAB实现
- CoreData的NSManagedObjectContext创建
- Ubuntu 15.10安装ns2.35+nam
- 网络爬虫——爬百度贴吧
- 关系型数据库管理系统简介