@@ -27,6 +27,8 @@ import (
27
27
"os"
28
28
"runtime"
29
29
"strings"
30
+
31
+ "github.com/astaxie/bat/httplib"
30
32
)
31
33
32
34
const version = "0.0.1"
@@ -57,6 +59,28 @@ func init() {
57
59
jsonmap = make (map [string ]interface {})
58
60
}
59
61
62
+ func formatResponseBody (res * http.Response , httpreq * httplib.BeegoHttpRequest , pretty bool ) string {
63
+ str , err := httpreq .String ()
64
+ if err != nil {
65
+ log .Fatalln ("can't get the url" , err )
66
+ }
67
+ fmt .Println ("" )
68
+ if pretty && strings .Contains (res .Header .Get ("Content-Type" ), contentJsonRegex ) {
69
+ var output interface {}
70
+ err = json .Unmarshal ([]byte (str ), & output )
71
+ if err != nil {
72
+ log .Fatal ("Response Json Unmarshal" , err )
73
+ }
74
+ bstr , err := json .MarshalIndent (output , "" , " " )
75
+ if err != nil {
76
+ log .Fatal ("Response Json MarshalIndent" , err )
77
+ }
78
+ str = string (bstr )
79
+ }
80
+
81
+ return str
82
+ }
83
+
60
84
func main () {
61
85
flag .Usage = usage
62
86
flag .Parse ()
@@ -155,42 +179,13 @@ func main() {
155
179
for k , v := range res .Header {
156
180
fmt .Println (Color (k , Gray ), ":" , Color (strings .Join (v , " " ), Cyan ))
157
181
}
158
- str , err := httpreq .String ()
159
- if err != nil {
160
- log .Fatalln ("can't get the url" , err )
161
- }
162
182
fmt .Println ("" )
163
- if pretty && strings .Contains (res .Header .Get ("Content-Type" ), contentJsonRegex ) {
164
- var output interface {}
165
- err = json .Unmarshal ([]byte (str ), & output )
166
- if err != nil {
167
- log .Fatal ("Response Json Unmarshal" , err )
168
- }
169
- bstr , err := json .MarshalIndent (output , "" , " " )
170
- if err != nil {
171
- log .Fatal ("Response Json MarshalIndent" , err )
172
- }
173
- str = string (bstr )
174
- }
175
- fmt .Println (ColorfulResponse (str ))
183
+
184
+ body := formatResponseBody (res , httpreq , pretty )
185
+ fmt .Println (ColorfulResponse (body ))
176
186
} else {
177
- str , err := httpreq .String ()
178
- if err != nil {
179
- log .Fatalln ("can't get the url" , err )
180
- }
181
- if pretty && strings .Contains (res .Header .Get ("Content-Type" ), contentJsonRegex ) {
182
- var output interface {}
183
- err = json .Unmarshal ([]byte (str ), & output )
184
- if err != nil {
185
- log .Fatal ("Response Json Unmarshal" , err )
186
- }
187
- bstr , err := json .MarshalIndent (output , "" , " " )
188
- if err != nil {
189
- log .Fatal ("Response Json MarshalIndent" , err )
190
- }
191
- str = string (bstr )
192
- }
193
- _ , err = os .Stdout .WriteString (str )
187
+ body := formatResponseBody (res , httpreq , pretty )
188
+ _ , err = os .Stdout .WriteString (body )
194
189
if err != nil {
195
190
log .Fatal (err )
196
191
}
@@ -203,24 +198,10 @@ func main() {
203
198
for k , v := range res .Header {
204
199
fmt .Println (k , ":" , strings .Join (v , " " ))
205
200
}
206
- str , err := httpreq .String ()
207
- if err != nil {
208
- log .Fatalln ("can't get the url" , err )
209
- }
210
201
fmt .Println ("" )
211
- if pretty && strings .Contains (res .Header .Get ("Content-Type" ), contentJsonRegex ) {
212
- var output interface {}
213
- err = json .Unmarshal ([]byte (str ), & output )
214
- if err != nil {
215
- log .Fatal ("Response Json Unmarshal" , err )
216
- }
217
- bstr , err := json .MarshalIndent (output , "" , " " )
218
- if err != nil {
219
- log .Fatal ("Response Json MarshalIndent" , err )
220
- }
221
- str = string (bstr )
222
- }
223
- fmt .Println (str )
202
+
203
+ body := formatResponseBody (res , httpreq , pretty )
204
+ fmt .Println (body )
224
205
}
225
206
}
226
207
0 commit comments