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

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))
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?