From cf1e770e1e13dee1428edcbf74079a53ac9e88f0 Mon Sep 17 00:00:00 2001 From: Matt Bell Date: Wed, 17 Dec 2014 16:20:24 -0800 Subject: [PATCH] commands/http: Client: decode chunked streaming output --- commands/http/client.go | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/commands/http/client.go b/commands/http/client.go index daaec0994..371ca8756 100644 --- a/commands/http/client.go +++ b/commands/http/client.go @@ -137,8 +137,31 @@ func getResponse(httpRes *http.Response, req cmds.Request) (cmds.Response, error contentType = strings.Split(contentType, ";")[0] if len(httpRes.Header.Get(streamHeader)) > 0 { + // if output is a stream, we can just use the body reader res.SetOutput(httpRes.Body) return res, nil + + } else if len(httpRes.Header.Get(channelHeader)) > 0 { + // if output is coming from a channel, decode each chunk + outChan := make(chan interface{}) + go func() { + dec := json.NewDecoder(httpRes.Body) + v := req.Command().Type + + for { + err := dec.Decode(&v) + if err != nil { + if err != io.EOF { + fmt.Println(err.Error()) + } + return + } + outChan <- v + } + }() + + res.SetOutput(outChan) + return res, nil } dec := json.NewDecoder(httpRes.Body)