dongyou2635 2018-03-29 22:11
浏览 139
已采纳

Golang中的原始HTTP响应

I have a request I'm making to an endpoint but however for some reason the response body only contains the last line of the response (the whole response is captured in fiddler). The same thing happens if I recreate the request in python using the requests module. However, I've noticed if I take the entire raw response in python, I am able to see all the lines (separated by multiple ). I'm wondering if it is possible to view the whole raw response in go like with the response.raw.data method in python. In other words is there a way I can view the whole text response instead of it cutting off everything but the last line? If anyone knows as to why the last line is being cut off it will be appreciated greatly as well.

To clarify, this only happens with this single endpoint and I suspect the s in the response body may be the culprit but I am unsure. I've not seen this behaviour from any other http response.

edit: this is the code I'm using to view the response

bodyB, _ := ioutil.ReadAll(resp.Body)
bodyStr := string(bodyB)
  • 写回答

1条回答 默认 最新

  • dpxpz37157 2018-03-29 22:31
    关注

    is a carriage return, but not a new line, so when you print it you are getting all of the lines, but they get overwritten each time.

    You probably will want to do:

    bodyB, _ := ioutil.ReadAll(resp.Body)
    bodyStr := string(bytes.Replace(bodyB, []byte(""), []byte("
    "), -1))
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛