-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
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
Conversation
2 similar comments
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() |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK, I will update.
@subchen See if you also update the docs https://echo.labstack.com/guide/response#send-json? |
@vishr, Guide document updated, please help review. |
There was a problem hiding this 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.
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 add
echo.Debug=true
or usec.JSONPretty()
, rebuild and redeploy. It is not accepted for us in a deployed box.So I bring a dynamic pretty response for
JSON
andXML
.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:
It works on all APIs, includes
GET
,POST
, etc.