I send request to server and get answer from one. You can see my code:
// Some code
queryUrl, err := url.Parse(someValidStringUrl)
resp, err := client.Get(queryUrl.String())
Let's check err
:
if err != nil {
log.Panic("Responce:", resp, "\nError:", err)
}
After this I want get body of my response. I make it such way but I get empty byte slice:
var bytes []byte
n, err := resp.Body.Read(bytes)
if err != nil {
log.Panic(err)
}
log.Println(n, string(bytes))
Example of output:
2017/07/13 16:32:36 0
Here I found another way:
// Some code
queryUrl, err := url.Parse(someValidStringUrl)
resp, err := client.Get(queryUrl.String())
if err != nil {
log.Panic("Responce:", resp, "\nError:", err)
}
defer resp.Body.Close()
if resp.StatusCode == 200 { // OK
bodyBytes, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Panic(err)
}
log.Println(string(bodyBytes))
}
Example of output:
<html lang='en'>
<head>
</head>
<body>
<title>Hello world</title>
</body>
</html>
Please, help me. What differences between such examples? Why do my way not work?