(go newbie here) I have some code like this:
func (c *clientImpl) queryHost(qtype string, page int) (io.ReadCloser, error) {
queryURI := c.URL + "/api/query"
req, _ := http.NewRequest(http.MethodGet, queryURI, nil)
req.Header.Add(authHeaderName, authToken)
req.Header.Add("Accept", accept)
q := req.URL.Query()
q.Add("type", qtype)
q.Add("pageSize", pageSize)
q.Add("page", strconv.Itoa(page))
req.URL.RawQuery = q.Encode()
resp, err := c.client.Do(req) //*http.Client
if err != nil {
return nil, utils.NestedError("Error in querying Host", err)
}
return resp.Body, nil
}
And then called like this:
body, err := c.queryHost(myQType, i)
if err != nil {
log.Printf("Error while trying to get page %v - %v \n", i, err)
return err
}
defer body.Close()
var qResult myQueryResult
err = xml.NewDecoder(body).Decode(&myQueryResult)
if err != nil {
log.Printf("Error while decoding page %v - %v \n", i, err)
return utils.NestedError("Error in decoding response body xml", err)
}
I want to log the original xml that was failed to be processed, but since this is a ReadCloser
and once read, it cannot be read again (or am I wrong?). Is there a way I can get this, or do I have to store the initial response somewhere and pass copies around?