Help need on ioutil.ReadAll

Hi All,

I am new to go programming. What I am trying to do is very simple. I just giving a http request and and getting response properly. After getting response I just trying to convert response body to byte array (using ioutil.ReadAll )and assign it to local function variable.
When when I am trying to debug it just after ioutil.ReadAll debugger get lossed and never back. Below is my code.


func startProcessing(){
 url := api.BuildURL(c.BaseURL, c.EndPoint)
	log.Print("hitting url:" + url)
	resp, err := api.SendGetRequest(url, c.SecurityToken)
	defer resp.Body.Close()
	if err != nil {
		log.Fatalln("Error sending Get request to " + c.LogSourceName)
	}
	body, err := ioutil.ReadAll(resp.Body)
   }

This complete function is launched as a go routine. And resp object I am getting is correct with 200 OK state. In body what I am getting is as below: (image attache with name)

<io.ReadCloser>
data:<*net/http.gzipReader>
body:<*net/http.bodyEOFSignal>
zr:nil <*compress/gzip.Reader>
zerr:

Can someone help me here?

You code seems correct (at least the part you have posted). What is your problem exactly? Do you get an error from io.ReadAll?

Be careful not to set the header “Content-Encoding: gzip” manually in your api.SendGetRequest, or you will have to uncompress the response manually. Compression is transparently handled by the Transport object in your HTTP client.

Thanks Giulio for quick response.
Actually what issue I am facing is when I put break point at
body, err := ioutil.ReadAll(resp.Body)
It’s coming till this point. But after that when I press F5/F10. Debug pointer disappear (as if something heavy running in background and never come back). And program stuck somewhere.
I am using VSCode as my IDE and delve debugger.

You only talk about a problem you have while debugging. Does your code work as intended when you don’t debug

@lutzhorn Nope it’s not. Just hang up when I launch from build “main.exe”

Hi All,

Thanks for quick response on this. I just resolve this issue by putting some sleep time on my main routine. What I can understand is it’s more effective way to make routine pause on instead of looping it.

–Pranay

Sorry but no. If sleep is the answer, the answer is wrong.

Maybe you can post your code (shortened) in full form (that can be run completely. This way we can guide you in the right direction.

It’s kinda sorta the answer if the problem was a for {} blocking the runtime. But there are of course better answers (your main should probably do something or wait for something rather than just looping) and posting the actual, full, code is definitely the thing - otherwise you are hiding things from the people who are trying to help you, frustrating us and leaving you without help.

1 Like

if your method SendGetRequest does not close the connect. ioutil.ReadAll method will not back. the Comments of method ReadAll has Underlined that.

1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.