I've done quiet a bit of searching and I haven't really found any answers to this question. How do you download a chunked response with Go. I'm more familiar with python. In python I would do something like this
with closing(requests.get('some_url' ,stream=True)) as res:
for chunk in res.iter_content(chunk_size=512):
#do something with chunk
I've tried a few different ways with go to this effect. Here is my latest attempt.
func DownloadLogs(start int, end int) {
url := "removed for security"
client := &http.Client{}
req, _ := http.NewRequest("GET", url, nil)
req.Header.Set("Content-Type", "application/json")
req.Header.Set("X-Auth-Key", "removed for security")
req.Header.Set("X-Auth-Email", "removed for security")
res, err := client.Do(req)
if err != nil {
panic(err.Error())
}
defer res.Body.Close()
// output, err := os.Create("test.txt")
// if err != nil {
// fmt.Println("Error while creating", "test", "-", err)
// return
// }
// defer output.Close()
reader := bufio.NewReader(res.Body)
for {
line, err := reader.ReadBytes('
')
if err != nil {
return
}
println(string(line))
}
}
I'm using Go's http library, but i've also tried go's napping library.