简体   繁体   English

Golang中的原始HTTP响应

[英]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. 如果我使用请求模块在python中重新创建请求,也会发生同样的事情。 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 ). 但是,我注意到,如果我在python中获取整个原始响应,我将能够看到所有行(由多个\\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. 我想知道是否可以像使用python中的response.raw.data方法一样查看整个原始响应。 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 \\r s in the response body may be the culprit but I am unsure. 为了澄清,这仅在单个端点上发生,我怀疑响应主体中的\\r可能是罪魁祸首,但我不确定。 I've not seen this behaviour from any other http response. 我没有从任何其他http响应中看到此行为。

edit: this is the code I'm using to view the response 编辑:这是我用来查看响应的代码

bodyB, _ := ioutil.ReadAll(resp.Body)
bodyStr := string(bodyB)

\\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. \\r是回车符,但不是新行,因此在打印时,您会得到所有行,但是每次都会覆盖它们。

You probably will want to do: 您可能会想做:

bodyB, _ := ioutil.ReadAll(resp.Body)
bodyStr := string(bytes.Replace(bodyB, []byte("\r"), []byte("\r\n"), -1))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM