From 90e8e1d97be2a76f4966e5d5dddc02e3fc0ea829 Mon Sep 17 00:00:00 2001 From: Eric Connell Date: Fri, 1 Aug 2014 14:11:51 -0600 Subject: [PATCH] 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