Skip to content

Automatically use JSONPretty/XMLPretty if '?pretty' in querystring #916

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Apr 28, 2017

Conversation

subchen
Copy link
Contributor

@subchen subchen commented Apr 27, 2017

We usually use c.JSON() to response a json object, but it outputs a compact json format as default, that are not readable.

As debug purpose, we maybe want to get a pretty json format for readable. Then, we have to change code to addecho.Debug=true or use c.JSONPretty(), rebuild and redeploy. It is not accepted for us in a deployed box.

So I bring a dynamic pretty response for JSON and XML.
In code, I will always use c.JSON() to output json. When I want to get a pretty json response, I only need add a ?pretty into querystring in my request URL, no any changes for my application.

Example:

# get default json
curl -fSL http://127.0.0.1:8080/v1/test

{"error":500,"message":"unable to get status","casue":"file not found: /tmp/data"}

# get pretty json
curl -fSL http://127.0.0.1:8080/v1/test?pretty

{
    "error":500,
    "message":"unable to get status",
    "casue":"file not found: /tmp/data"
}

It works on all APIs, includes GET, POST, etc.

@coveralls
Copy link

Coverage Status

Coverage increased (+0.05%) to 83.76% when pulling 950dc52 on subchen:auto-pretty into 1049c96 on labstack:master.

2 similar comments
@coveralls
Copy link

Coverage Status

Coverage increased (+0.05%) to 83.76% when pulling 950dc52 on subchen:auto-pretty into 1049c96 on labstack:master.

@coveralls
Copy link

Coverage Status

Coverage increased (+0.05%) to 83.76% when pulling 950dc52 on subchen:auto-pretty into 1049c96 on labstack:master.

@coveralls
Copy link

coveralls commented Apr 27, 2017

Coverage Status

Coverage increased (+0.2%) to 83.946% when pulling 45f3428 on subchen:auto-pretty into 1049c96 on labstack:master.

@vishr vishr self-requested a review April 27, 2017 18:18
context.go Outdated
@@ -385,7 +385,10 @@ func (c *context) String(code int, s string) (err error) {
}

func (c *context) JSON(code int, i interface{}) (err error) {
if c.echo.Debug {
query := c.request.URL.Query()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can directly use _, pretty := c.QueryParams()["pretty"]. Just for consistency please rename the isPretty to pretty.

PS: Same for XML

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, I will update.

@vishr
Copy link
Member

vishr commented Apr 27, 2017

@subchen See if you also update the docs https://echo.labstack.com/guide/response#send-json?

@subchen
Copy link
Contributor Author

subchen commented Apr 28, 2017

@vishr, Guide document updated, please help review.
Thanks

@coveralls
Copy link

coveralls commented Apr 28, 2017

Coverage Status

Coverage increased (+0.2%) to 83.916% when pulling bf47174 on subchen:auto-pretty into 1049c96 on labstack:master.

Copy link
Member

@vishr vishr left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for your contribution.

@vishr vishr merged commit 0dfcb31 into labstack:master Apr 28, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants