我正在尝试连接到一个执行json数据流的端点。我想知道如何使用Go的net/http包执行基本请求,并在响应出现时读取响应?目前,我只能在连接关闭时读取响应。
resp, err := http.Get("localhost:8080/stream")
if err != nil {
...
}
...
// perform work while connected and getting data
任何帮助都将不胜感激!谢谢!
我正在尝试连接到一个执行json数据流的端点。我想知道如何使用Go的net/http包执行基本请求,并在响应出现时读取响应?目前,我只能在连接关闭时读取响应。
resp, err := http.Get("localhost:8080/stream")
if err != nil {
...
}
...
// perform work while connected and getting data
任何帮助都将不胜感激!谢谢!
The way to do streaming JSON parsing is with a Decoder:
json.NewDecoder(resp.Body).Decode(&yourStuff)
For a streaming API where it's a bunch of objects coming back (a la Twitter), that should stream great with this model and the built-in encoding/json API. But if it's a large response where you have an object that's got a giant array with 10MB of stuff, you probably need to write your own Decoder to pull those inner pieces out and return them. I'm running into that problem with a library I've written.