mirror of
https://github.com/labstack/echo.git
synced 2025-07-07 01:06:40 +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:
@ -385,7 +385,8 @@ func (c *context) String(code int, s string) (err error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (c *context) JSON(code int, i interface{}) (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, " ")
|
return c.JSONPretty(code, i, " ")
|
||||||
}
|
}
|
||||||
b, err := json.Marshal(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) {
|
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, " ")
|
return c.XMLPretty(code, i, " ")
|
||||||
}
|
}
|
||||||
b, err := xml.Marshal(i)
|
b, err := xml.Marshal(i)
|
||||||
|
@ -73,10 +73,22 @@ func TestContext(t *testing.T) {
|
|||||||
assert.Equal(t, userJSON, rec.Body.String())
|
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
|
// JSONPretty
|
||||||
rec = httptest.NewRecorder()
|
rec = httptest.NewRecorder()
|
||||||
c = e.NewContext(req, rec).(*context)
|
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) {
|
if assert.NoError(t, err) {
|
||||||
assert.Equal(t, http.StatusOK, rec.Code)
|
assert.Equal(t, http.StatusOK, rec.Code)
|
||||||
assert.Equal(t, MIMEApplicationJSONCharsetUTF8, rec.Header().Get(HeaderContentType))
|
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())
|
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)
|
// XML (error)
|
||||||
rec = httptest.NewRecorder()
|
rec = httptest.NewRecorder()
|
||||||
c = e.NewContext(req, rec).(*context)
|
c = e.NewContext(req, rec).(*context)
|
||||||
@ -119,7 +143,7 @@ func TestContext(t *testing.T) {
|
|||||||
// XMLPretty
|
// XMLPretty
|
||||||
rec = httptest.NewRecorder()
|
rec = httptest.NewRecorder()
|
||||||
c = e.NewContext(req, rec).(*context)
|
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) {
|
if assert.NoError(t, err) {
|
||||||
assert.Equal(t, http.StatusOK, rec.Code)
|
assert.Equal(t, http.StatusOK, rec.Code)
|
||||||
assert.Equal(t, MIMEApplicationXMLCharsetUTF8, rec.Header().Get(HeaderContentType))
|
assert.Equal(t, MIMEApplicationXMLCharsetUTF8, rec.Header().Get(HeaderContentType))
|
||||||
|
@ -31,13 +31,13 @@ const (
|
|||||||
)
|
)
|
||||||
|
|
||||||
const userJSONPretty = `{
|
const userJSONPretty = `{
|
||||||
"id": 1,
|
"id": 1,
|
||||||
"name": "Jon Snow"
|
"name": "Jon Snow"
|
||||||
}`
|
}`
|
||||||
|
|
||||||
const userXMLPretty = `<user>
|
const userXMLPretty = `<user>
|
||||||
<id>1</id>
|
<id>1</id>
|
||||||
<name>Jon Snow</name>
|
<name>Jon Snow</name>
|
||||||
</user>`
|
</user>`
|
||||||
|
|
||||||
func TestEcho(t *testing.T) {
|
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
|
### JSON Blob
|
||||||
|
|
||||||
`Context#JSONBlob(code int, b []byte)` can be used to send pre-encoded JSON blob directly
|
`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>
|
</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
|
### XML Blob
|
||||||
|
|
||||||
`Context#XMLBlob(code int, b []byte)` can be used to send pre-encoded XML blob directly
|
`Context#XMLBlob(code int, b []byte)` can be used to send pre-encoded XML blob directly
|
||||||
|
Reference in New Issue
Block a user