mirror of
https://github.com/labstack/echo.git
synced 2024-12-24 20:14:31 +02:00
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
This commit is contained in:
parent
1049c9613c
commit
0dfcb31d9e
@ -385,7 +385,8 @@ func (c *context) String(code int, s string) (err error) {
|
||||
}
|
||||
|
||||
func (c *context) JSON(code int, i interface{}) (err error) {
|
||||
if c.echo.Debug {
|
||||
_, pretty := c.request.URL.Query()["pretty"]
|
||||
if c.echo.Debug || pretty {
|
||||
return c.JSONPretty(code, i, " ")
|
||||
}
|
||||
b, err := json.Marshal(i)
|
||||
@ -429,7 +430,8 @@ func (c *context) JSONPBlob(code int, callback string, b []byte) (err error) {
|
||||
}
|
||||
|
||||
func (c *context) XML(code int, i interface{}) (err error) {
|
||||
if c.echo.Debug {
|
||||
_, pretty := c.request.URL.Query()["pretty"]
|
||||
if c.echo.Debug || pretty {
|
||||
return c.XMLPretty(code, i, " ")
|
||||
}
|
||||
b, err := xml.Marshal(i)
|
||||
|
@ -73,10 +73,22 @@ func TestContext(t *testing.T) {
|
||||
assert.Equal(t, userJSON, rec.Body.String())
|
||||
}
|
||||
|
||||
// JSON with "?pretty"
|
||||
req = httptest.NewRequest(GET, "/?pretty", nil)
|
||||
rec = httptest.NewRecorder()
|
||||
c = e.NewContext(req, rec).(*context)
|
||||
err = c.JSON(http.StatusOK, user{1, "Jon Snow"})
|
||||
if assert.NoError(t, err) {
|
||||
assert.Equal(t, http.StatusOK, rec.Code)
|
||||
assert.Equal(t, MIMEApplicationJSONCharsetUTF8, rec.Header().Get(HeaderContentType))
|
||||
assert.Equal(t, userJSONPretty, rec.Body.String())
|
||||
}
|
||||
req = httptest.NewRequest(GET, "/", nil) // reset
|
||||
|
||||
// JSONPretty
|
||||
rec = httptest.NewRecorder()
|
||||
c = e.NewContext(req, rec).(*context)
|
||||
err = c.JSONPretty(http.StatusOK, user{1, "Jon Snow"}, "\t")
|
||||
err = c.JSONPretty(http.StatusOK, user{1, "Jon Snow"}, " ")
|
||||
if assert.NoError(t, err) {
|
||||
assert.Equal(t, http.StatusOK, rec.Code)
|
||||
assert.Equal(t, MIMEApplicationJSONCharsetUTF8, rec.Header().Get(HeaderContentType))
|
||||
@ -110,6 +122,18 @@ func TestContext(t *testing.T) {
|
||||
assert.Equal(t, xml.Header+userXML, rec.Body.String())
|
||||
}
|
||||
|
||||
// XML with "?pretty"
|
||||
req = httptest.NewRequest(GET, "/?pretty", nil)
|
||||
rec = httptest.NewRecorder()
|
||||
c = e.NewContext(req, rec).(*context)
|
||||
err = c.XML(http.StatusOK, user{1, "Jon Snow"})
|
||||
if assert.NoError(t, err) {
|
||||
assert.Equal(t, http.StatusOK, rec.Code)
|
||||
assert.Equal(t, MIMEApplicationXMLCharsetUTF8, rec.Header().Get(HeaderContentType))
|
||||
assert.Equal(t, xml.Header+userXMLPretty, rec.Body.String())
|
||||
}
|
||||
req = httptest.NewRequest(GET, "/", nil)
|
||||
|
||||
// XML (error)
|
||||
rec = httptest.NewRecorder()
|
||||
c = e.NewContext(req, rec).(*context)
|
||||
@ -119,7 +143,7 @@ func TestContext(t *testing.T) {
|
||||
// XMLPretty
|
||||
rec = httptest.NewRecorder()
|
||||
c = e.NewContext(req, rec).(*context)
|
||||
err = c.XMLPretty(http.StatusOK, user{1, "Jon Snow"}, "\t")
|
||||
err = c.XMLPretty(http.StatusOK, user{1, "Jon Snow"}, " ")
|
||||
if assert.NoError(t, err) {
|
||||
assert.Equal(t, http.StatusOK, rec.Code)
|
||||
assert.Equal(t, MIMEApplicationXMLCharsetUTF8, rec.Header().Get(HeaderContentType))
|
||||
|
@ -31,13 +31,13 @@ const (
|
||||
)
|
||||
|
||||
const userJSONPretty = `{
|
||||
"id": 1,
|
||||
"name": "Jon Snow"
|
||||
"id": 1,
|
||||
"name": "Jon Snow"
|
||||
}`
|
||||
|
||||
const userXMLPretty = `<user>
|
||||
<id>1</id>
|
||||
<name>Jon Snow</name>
|
||||
<id>1</id>
|
||||
<name>Jon Snow</name>
|
||||
</user>`
|
||||
|
||||
func TestEcho(t *testing.T) {
|
||||
|
@ -108,6 +108,33 @@ func(c echo.Context) error {
|
||||
}
|
||||
```
|
||||
|
||||
Today, `Context#JSON(code int, i interface{})` also can output a pretty printed JSON
|
||||
(indented with spaces) when a querystring `?pretty` is attached in request URL.
|
||||
|
||||
*Example*
|
||||
|
||||
```go
|
||||
func(c echo.Context) error {
|
||||
u := &User{
|
||||
Name: "Jon",
|
||||
Email: "joe@labstack.com",
|
||||
}
|
||||
return c.JSON(http.StatusOK, u)
|
||||
}
|
||||
```
|
||||
|
||||
```bash
|
||||
curl -fSL http://127.0.0.1:8080/v1/users/123?pretty
|
||||
```
|
||||
|
||||
```js
|
||||
{
|
||||
"email": "joe@labstack.com",
|
||||
"name": "Jon"
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
### JSON Blob
|
||||
|
||||
`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 {
|
||||
</User>
|
||||
```
|
||||
|
||||
Today, `Context#XML(code int, i interface{})` also can output a pretty printed XML
|
||||
(indented with spaces) when a querystring `?pretty` is attached in request URL.
|
||||
|
||||
*Example*
|
||||
|
||||
```go
|
||||
func(c echo.Context) error {
|
||||
u := &User{
|
||||
Name: "Jon",
|
||||
Email: "joe@labstack.com",
|
||||
}
|
||||
return c.XML(http.StatusOK, u)
|
||||
}
|
||||
```
|
||||
|
||||
```bash
|
||||
curl -fSL http://127.0.0.1:8080/v1/users/123?pretty
|
||||
```
|
||||
|
||||
```xml
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<User>
|
||||
<Name>Jon</Name>
|
||||
<Email>joe@labstack.com</Email>
|
||||
</User>
|
||||
```
|
||||
|
||||
### XML Blob
|
||||
|
||||
`Context#XMLBlob(code int, b []byte)` can be used to send pre-encoded XML blob directly
|
||||
|
Loading…
Reference in New Issue
Block a user