Skip to content

Commit 0dfcb31

Browse files
subchenvishr
authored andcommitted
Automatically use JSONPretty/XMLPretty if '?pretty' in querystring (#916)
* Automatically use JSONPretty/XMLPretty if '?pretty' in querystring * Update unit test cases * Simplify code according comments * Update guide for pretty json/xml
1 parent 1049c96 commit 0dfcb31

File tree

4 files changed

+88
-8
lines changed

4 files changed

+88
-8
lines changed

context.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -385,7 +385,8 @@ func (c *context) String(code int, s string) (err error) {
385385
}
386386

387387
func (c *context) JSON(code int, i interface{}) (err error) {
388-
if c.echo.Debug {
388+
_, pretty := c.request.URL.Query()["pretty"]
389+
if c.echo.Debug || pretty {
389390
return c.JSONPretty(code, i, " ")
390391
}
391392
b, err := json.Marshal(i)
@@ -429,7 +430,8 @@ func (c *context) JSONPBlob(code int, callback string, b []byte) (err error) {
429430
}
430431

431432
func (c *context) XML(code int, i interface{}) (err error) {
432-
if c.echo.Debug {
433+
_, pretty := c.request.URL.Query()["pretty"]
434+
if c.echo.Debug || pretty {
433435
return c.XMLPretty(code, i, " ")
434436
}
435437
b, err := xml.Marshal(i)

context_test.go

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,10 +73,22 @@ func TestContext(t *testing.T) {
7373
assert.Equal(t, userJSON, rec.Body.String())
7474
}
7575

76+
// JSON with "?pretty"
77+
req = httptest.NewRequest(GET, "/?pretty", nil)
78+
rec = httptest.NewRecorder()
79+
c = e.NewContext(req, rec).(*context)
80+
err = c.JSON(http.StatusOK, user{1, "Jon Snow"})
81+
if assert.NoError(t, err) {
82+
assert.Equal(t, http.StatusOK, rec.Code)
83+
assert.Equal(t, MIMEApplicationJSONCharsetUTF8, rec.Header().Get(HeaderContentType))
84+
assert.Equal(t, userJSONPretty, rec.Body.String())
85+
}
86+
req = httptest.NewRequest(GET, "/", nil) // reset
87+
7688
// JSONPretty
7789
rec = httptest.NewRecorder()
7890
c = e.NewContext(req, rec).(*context)
79-
err = c.JSONPretty(http.StatusOK, user{1, "Jon Snow"}, "\t")
91+
err = c.JSONPretty(http.StatusOK, user{1, "Jon Snow"}, " ")
8092
if assert.NoError(t, err) {
8193
assert.Equal(t, http.StatusOK, rec.Code)
8294
assert.Equal(t, MIMEApplicationJSONCharsetUTF8, rec.Header().Get(HeaderContentType))
@@ -110,6 +122,18 @@ func TestContext(t *testing.T) {
110122
assert.Equal(t, xml.Header+userXML, rec.Body.String())
111123
}
112124

125+
// XML with "?pretty"
126+
req = httptest.NewRequest(GET, "/?pretty", nil)
127+
rec = httptest.NewRecorder()
128+
c = e.NewContext(req, rec).(*context)
129+
err = c.XML(http.StatusOK, user{1, "Jon Snow"})
130+
if assert.NoError(t, err) {
131+
assert.Equal(t, http.StatusOK, rec.Code)
132+
assert.Equal(t, MIMEApplicationXMLCharsetUTF8, rec.Header().Get(HeaderContentType))
133+
assert.Equal(t, xml.Header+userXMLPretty, rec.Body.String())
134+
}
135+
req = httptest.NewRequest(GET, "/", nil)
136+
113137
// XML (error)
114138
rec = httptest.NewRecorder()
115139
c = e.NewContext(req, rec).(*context)
@@ -119,7 +143,7 @@ func TestContext(t *testing.T) {
119143
// XMLPretty
120144
rec = httptest.NewRecorder()
121145
c = e.NewContext(req, rec).(*context)
122-
err = c.XMLPretty(http.StatusOK, user{1, "Jon Snow"}, "\t")
146+
err = c.XMLPretty(http.StatusOK, user{1, "Jon Snow"}, " ")
123147
if assert.NoError(t, err) {
124148
assert.Equal(t, http.StatusOK, rec.Code)
125149
assert.Equal(t, MIMEApplicationXMLCharsetUTF8, rec.Header().Get(HeaderContentType))

echo_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,13 @@ const (
3131
)
3232

3333
const userJSONPretty = `{
34-
"id": 1,
35-
"name": "Jon Snow"
34+
"id": 1,
35+
"name": "Jon Snow"
3636
}`
3737

3838
const userXMLPretty = `<user>
39-
<id>1</id>
40-
<name>Jon Snow</name>
39+
<id>1</id>
40+
<name>Jon Snow</name>
4141
</user>`
4242

4343
func TestEcho(t *testing.T) {

website/content/guide/response.md

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,33 @@ func(c echo.Context) error {
108108
}
109109
```
110110

111+
Today, `Context#JSON(code int, i interface{})` also can output a pretty printed JSON
112+
(indented with spaces) when a querystring `?pretty` is attached in request URL.
113+
114+
*Example*
115+
116+
```go
117+
func(c echo.Context) error {
118+
u := &User{
119+
Name: "Jon",
120+
Email: "joe@labstack.com",
121+
}
122+
return c.JSON(http.StatusOK, u)
123+
}
124+
```
125+
126+
```bash
127+
curl -fSL http://127.0.0.1:8080/v1/users/123?pretty
128+
```
129+
130+
```js
131+
{
132+
"email": "joe@labstack.com",
133+
"name": "Jon"
134+
}
135+
```
136+
137+
111138
### JSON Blob
112139

113140
`Context#JSONBlob(code int, b []byte)` can be used to send pre-encoded JSON blob directly
@@ -191,6 +218,33 @@ func(c echo.Context) error {
191218
</User>
192219
```
193220

221+
Today, `Context#XML(code int, i interface{})` also can output a pretty printed XML
222+
(indented with spaces) when a querystring `?pretty` is attached in request URL.
223+
224+
*Example*
225+
226+
```go
227+
func(c echo.Context) error {
228+
u := &User{
229+
Name: "Jon",
230+
Email: "joe@labstack.com",
231+
}
232+
return c.XML(http.StatusOK, u)
233+
}
234+
```
235+
236+
```bash
237+
curl -fSL http://127.0.0.1:8080/v1/users/123?pretty
238+
```
239+
240+
```xml
241+
<?xml version="1.0" encoding="UTF-8"?>
242+
<User>
243+
<Name>Jon</Name>
244+
<Email>joe@labstack.com</Email>
245+
</User>
246+
```
247+
194248
### XML Blob
195249

196250
`Context#XMLBlob(code int, b []byte)` can be used to send pre-encoded XML blob directly

0 commit comments

Comments
 (0)