Skip to content

Commit

Permalink
bug: fixes issue with printing empty body #362
Browse files Browse the repository at this point in the history
  • Loading branch information
srinandan committed Dec 19, 2023
1 parent 91a92b9 commit 58b508f
Showing 1 changed file with 13 additions and 3 deletions.
16 changes: 13 additions & 3 deletions internal/apiclient/httpclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -281,8 +281,18 @@ func HttpClient(params ...string) (respBody []byte, err error) {
case 1:
req, err = http.NewRequest(http.MethodGet, params[0], nil)
case 2:
payload, _ := PrettifyJSON([]byte(params[1]))
clilog.Debug.Println("Payload: ", string(payload))
payload := []byte(params[1])
if len(payload) > 0 {
//attempt to convert to json
jsonPayload, err := PrettifyJSON([]byte(params[1]))
if err != nil {
//payload is not json, print as-is
clilog.Debug.Println("Payload: ", string(payload))
} else {
//print json
clilog.Debug.Println("Payload: ", string(jsonPayload))
}
}
req, err = http.NewRequest(http.MethodPost, params[0], bytes.NewBuffer([]byte(params[1])))
case 3:
if req, err = getRequest(params); err != nil {
Expand Down Expand Up @@ -346,7 +356,7 @@ func PrettifyJSON(body []byte) ([]byte, error) {
var prettyJSON bytes.Buffer
err := json.Indent(&prettyJSON, body, "", "\t")
if err != nil {
clilog.Error.Println("error parsing response: ", err)
//fail silently
return nil, err
}
return prettyJSON.Bytes(), nil
Expand Down

0 comments on commit 58b508f

Please sign in to comment.