mirror of https://github.com/hashicorp/consul
added URL query parameter of "pretty=true" to output formatted json from the HTTP API
parent
4953553979
commit
90e8e1d97b
|
@ -1,7 +1,6 @@
|
||||||
package agent
|
package agent
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"github.com/hashicorp/consul/consul/structs"
|
"github.com/hashicorp/consul/consul/structs"
|
||||||
"github.com/mitchellh/mapstructure"
|
"github.com/mitchellh/mapstructure"
|
||||||
|
@ -138,15 +137,25 @@ func (s *HTTPServer) wrap(handler func(resp http.ResponseWriter, req *http.Reque
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var prettyPrint bool
|
||||||
|
if req.URL.Query().Get("pretty") == "true" {
|
||||||
|
prettyPrint = true
|
||||||
|
} else {
|
||||||
|
prettyPrint = false
|
||||||
|
}
|
||||||
// Write out the JSON object
|
// Write out the JSON object
|
||||||
if obj != nil {
|
if obj != nil {
|
||||||
var buf bytes.Buffer
|
var buf []byte
|
||||||
enc := json.NewEncoder(&buf)
|
if prettyPrint == true {
|
||||||
if err = enc.Encode(obj); err != nil {
|
buf, err = json.MarshalIndent(obj, "", " ")
|
||||||
|
} else {
|
||||||
|
buf, err = json.Marshal(obj)
|
||||||
|
}
|
||||||
|
if err != nil {
|
||||||
goto HAS_ERR
|
goto HAS_ERR
|
||||||
}
|
}
|
||||||
resp.Header().Set("Content-Type", "application/json")
|
resp.Header().Set("Content-Type", "application/json")
|
||||||
resp.Write(buf.Bytes())
|
resp.Write(buf)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return f
|
return f
|
||||||
|
|
Loading…
Reference in New Issue