Skip to content

Commit 5ed1f8a

Browse files
committed
Merge pull request astaxie#15 from glkz/json_indent
Use json.Indent for formatting
2 parents d27cd05 + e4b5f4f commit 5ed1f8a

File tree

1 file changed

+8
-10
lines changed

1 file changed

+8
-10
lines changed

http.go

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package main
22

33
import (
4+
"bytes"
45
"encoding/json"
56
"fmt"
67
"io/ioutil"
@@ -104,23 +105,20 @@ func getHTTP(method string, url string, args []string) (r *httplib.BeegoHttpRequ
104105
}
105106

106107
func formatResponseBody(res *http.Response, httpreq *httplib.BeegoHttpRequest, pretty bool) string {
107-
str, err := httpreq.String()
108+
body, err := httpreq.Bytes()
108109
if err != nil {
109110
log.Fatalln("can't get the url", err)
110111
}
111112
fmt.Println("")
112113
if pretty && strings.Contains(res.Header.Get("Content-Type"), contentJsonRegex) {
113-
var output interface{}
114-
err = json.Unmarshal([]byte(str), &output)
114+
var output bytes.Buffer
115+
err := json.Indent(&output, body, "", " ")
115116
if err != nil {
116-
log.Fatal("Response Json Unmarshal", err)
117+
log.Fatal("Response Json Indent: ", err)
117118
}
118-
bstr, err := json.MarshalIndent(output, "", " ")
119-
if err != nil {
120-
log.Fatal("Response Json MarshalIndent", err)
121-
}
122-
str = string(bstr)
119+
120+
return output.String()
123121
}
124122

125-
return str
123+
return string(body)
126124
}

0 commit comments

Comments
 (0)