Do I copy resp.Body?

Issue

I am learning go and I have the following code which works fine:

resp, err := http.Get(url)  // get the html
   ...
doc, err := html.Parse(resp.Body)  // parse the html page

Now I want to print out the html first then do the parsing:

resp, err := http.Get(url)
   ...
b, err := ioutil.ReadAll(resp.Body)  // this line is added, not working now...
doc, err := html.Parse(resp.Body)

I guess the reason is resp.Body is a reader, I can not call the read twice? Any idea how can I do this correctly? Copy the resp.Body?

Solution

Because the client streams the response body from the network, it’s not possible to read the body twice.

Read the response to a []byte as you are already doing. Create a io.Reader on the bytes for the HTML parser using bytes.NewReader.

resp, err := http.Get(url)
...
b, err := io.ReadAll(resp.Body)  
doc, err := html.Parse(bytes.NewReader(b))

Answered By – Bayta Darell

Answer Checked By – Gilberto Lyons (GoLangFix Admin)

Leave a Reply

Your email address will not be published.