I am trying to connect to an endpoint that does http streaming of json data. I was wondering how to perform a basic request using Go's net/http package and read the response as it comes in. Currently, I am only able to read the response when the connection closes.
resp, err := http.Get("localhost:8080/stream")
if err != nil {
...
}
...
// perform work while connected and getting data
Any insight would be greatly appreciated!
Thanks!
-RC
The answer provided by Eve Freeman is the correct way to read json data. For reading any type of data, you can use the method below:
resp, err := http.Get("http://localhost:3000/stream")
...
reader := bufio.NewReader(resp.Body)
for {
line, err := reader.ReadBytes('\n')
...
log.Println(string(line))
}