Skip to content

Commit

Permalink
Support empty responses in request commands
Browse files Browse the repository at this point in the history
Avoid JSON parser when the response is empty - common cases for HTTP 204 in issues deletion, or moving issues to sprint.
  • Loading branch information
voiski authored and ldelossa committed Sep 7, 2020
1 parent ff5decc commit b572037
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions jiracmd/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit b572037

Please sign in to comment.