From 90e8e1d97be2a76f4966e5d5dddc02e3fc0ea829 Mon Sep 17 00:00:00 2001 From: Eric Connell Date: Fri, 1 Aug 2014 14:11:51 -0600 Subject: [PATCH 1/4] added URL query parameter of "pretty=true" to output formatted json from the HTTP API --- command/agent/http.go | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/command/agent/http.go b/command/agent/http.go index 792055b6bf..300d165fdb 100644 --- a/command/agent/http.go +++ b/command/agent/http.go @@ -1,7 +1,6 @@ package agent import ( - "bytes" "encoding/json" "github.com/hashicorp/consul/consul/structs" "github.com/mitchellh/mapstructure" @@ -138,15 +137,25 @@ func (s *HTTPServer) wrap(handler func(resp http.ResponseWriter, req *http.Reque return } + var prettyPrint bool + if req.URL.Query().Get("pretty") == "true" { + prettyPrint = true + } else { + prettyPrint = false + } // Write out the JSON object if obj != nil { - var buf bytes.Buffer - enc := json.NewEncoder(&buf) - if err = enc.Encode(obj); err != nil { + var buf []byte + if prettyPrint == true { + buf, err = json.MarshalIndent(obj, "", " ") + } else { + buf, err = json.Marshal(obj) + } + if err != nil { goto HAS_ERR } resp.Header().Set("Content-Type", "application/json") - resp.Write(buf.Bytes()) + resp.Write(buf) } } return f From ed6dd856dc967e799b6aa72cb67d8546bff7fdf4 Mon Sep 17 00:00:00 2001 From: Eric Connell Date: Fri, 1 Aug 2014 14:16:21 -0600 Subject: [PATCH 2/4] updated website documentation with the "pretty" parameter --- website/source/docs/agent/http.html.markdown | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/website/source/docs/agent/http.html.markdown b/website/source/docs/agent/http.html.markdown index c95662b05b..d5757fa58c 100644 --- a/website/source/docs/agent/http.html.markdown +++ b/website/source/docs/agent/http.html.markdown @@ -79,6 +79,12 @@ which is the last time a server was contacted by the leader node in milliseconds. The "X-Consul-KnownLeader" also indicates if there is a known leader. These can be used to gauge if a stale read should be used. +## Formatted JSON Output + +By default, the output of all HTTP API requests return minimized JSON with all +whitespace removed. By adding "?pretty=true" to the HTTP request URL, +formatted JSON will be returned. + ## KV The KV endpoint is used to expose a simple key/value store. This can be used From e3c405298259662fc5b9b4e6d99f8bc9f0edde05 Mon Sep 17 00:00:00 2001 From: Eric Connell Date: Fri, 1 Aug 2014 14:24:36 -0600 Subject: [PATCH 3/4] make pretty condition more readable --- command/agent/http.go | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/command/agent/http.go b/command/agent/http.go index 300d165fdb..cc37b2db9b 100644 --- a/command/agent/http.go +++ b/command/agent/http.go @@ -137,11 +137,9 @@ func (s *HTTPServer) wrap(handler func(resp http.ResponseWriter, req *http.Reque return } - var prettyPrint bool + prettyPrint := false if req.URL.Query().Get("pretty") == "true" { prettyPrint = true - } else { - prettyPrint = false } // Write out the JSON object if obj != nil { From a5775aae24e14df4b2a7b544b977bafd3507a276 Mon Sep 17 00:00:00 2001 From: Eric Connell Date: Fri, 1 Aug 2014 14:28:46 -0600 Subject: [PATCH 4/4] modified so ?pretty=anything will work --- command/agent/http.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/command/agent/http.go b/command/agent/http.go index cc37b2db9b..a254ecf195 100644 --- a/command/agent/http.go +++ b/command/agent/http.go @@ -138,13 +138,13 @@ func (s *HTTPServer) wrap(handler func(resp http.ResponseWriter, req *http.Reque } prettyPrint := false - if req.URL.Query().Get("pretty") == "true" { + if req.URL.Query().Get("pretty") != "" { prettyPrint = true } // Write out the JSON object if obj != nil { var buf []byte - if prettyPrint == true { + if prettyPrint { buf, err = json.MarshalIndent(obj, "", " ") } else { buf, err = json.Marshal(obj)