How do I read a streaming response body using Golang's net/http package?

huangapple go评论72阅读模式
英文:

How do I read a streaming response body using Golang's net/http package?

问题

我正在尝试连接到一个通过HTTP流式传输JSON数据的端点。我想知道如何使用Go的net/http包执行基本请求,并在数据到达时读取响应。目前,我只能在连接关闭时读取响应。

resp, err := http.Get("localhost:8080/stream")
if err != nil {
    // 错误处理
}
// 在连接和获取数据时执行工作

任何帮助都将不胜感激!

谢谢!

  • RC
英文:

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

答案1

得分: 47

Eve Freeman提供的答案是读取json数据的正确方法。对于读取任何类型的数据,您可以使用以下方法:

resp, err := http.Get("http://localhost:3000/stream")
...

reader := bufio.NewReader(resp.Body)
for {
    line, err := reader.ReadBytes('\n')
    ...

    log.Println(string(line))
}
英文:

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))
}

答案2

得分: 17

进行流式JSON解析的方法是使用解码器(Decoder):

json.NewDecoder(resp.Body).Decode(&yourStuff)

对于一个返回一堆对象的流式API(比如Twitter),这个模型和内置的encoding/json API应该能很好地进行流式处理。但是如果响应很大,其中有一个包含10MB数据的巨大数组的对象,你可能需要编写自己的解码器来提取并返回这些内部部分。我在我编写的一个库中遇到了这个问题。

英文:

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.

huangapple
  • 本文由 发表于 2014年3月1日 08:41:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/22108519.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定