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.
This commit is contained in:
Alan Voiski
2020-09-07 11:56:56 -07:00
committed by Louis DeLosSantos
parent ff5decc114
commit b572037cfe
+11 -2
View File
@@ -3,12 +3,12 @@ package jiracmd
import ( import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"io/ioutil"
"net/url" "net/url"
"strings" "strings"
"github.com/coryb/figtree" "github.com/coryb/figtree"
"github.com/coryb/oreo" "github.com/coryb/oreo"
"github.com/go-jira/jira/jiracli" "github.com/go-jira/jira/jiracli"
kingpin "gopkg.in/alecthomas/kingpin.v2" kingpin "gopkg.in/alecthomas/kingpin.v2"
) )
@@ -74,8 +74,17 @@ func CmdRequest(o *oreo.Client, globals *jiracli.GlobalOptions, opts *RequestOpt
} }
defer resp.Body.Close() 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{} 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 fmt.Errorf("JSON Parse Error: %v", err)
} }
return opts.PrintTemplate(&data) return opts.PrintTemplate(&data)