From b572037cfe119e5b72a48098484c76d10cd86bde Mon Sep 17 00:00:00 2001 From: Alan Voiski Date: Mon, 7 Sep 2020 11:56:56 -0700 Subject: [PATCH] Support empty responses in request commands Avoid JSON parser when the response is empty - common cases for HTTP 204 in issues deletion, or moving issues to sprint. --- jiracmd/request.go | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/jiracmd/request.go b/jiracmd/request.go index 3a83b325..38f1911d 100644 --- a/jiracmd/request.go +++ b/jiracmd/request.go @@ -3,12 +3,12 @@ package jiracmd import ( "encoding/json" "fmt" + "io/ioutil" "net/url" "strings" "github.com/coryb/figtree" "github.com/coryb/oreo" - "github.com/go-jira/jira/jiracli" kingpin "gopkg.in/alecthomas/kingpin.v2" ) @@ -74,8 +74,17 @@ func CmdRequest(o *oreo.Client, globals *jiracli.GlobalOptions, opts *RequestOpt } defer resp.Body.Close() + bodyBytes, err := ioutil.ReadAll(resp.Body) + if err != nil { + return fmt.Errorf("Response Body read Error: %v", err) + } + if len(bodyBytes) == 0 { + log.Info("Empty response for status %d", resp.StatusCode) + return nil + } + var data interface{} - if err := json.NewDecoder(resp.Body).Decode(&data); err != nil { + if err := json.Unmarshal(bodyBytes, &data); err != nil { return fmt.Errorf("JSON Parse Error: %v", err) } return opts.PrintTemplate(&data)