James Morgan
James Morgan

Reputation: 611

Raw http response in golang

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 \r). 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 \rs 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)

Upvotes: 1

Views: 5253

Answers (1)

dave
dave

Reputation: 64657

\r 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("\r"), []byte("\r\n"), -1))

Upvotes: 5

Related Questions