Skip to content

Instantly share code, notes, and snippets.

@ivey
Created August 26, 2015 21:53
Show Gist options
  • Save ivey/18dfeced31f3cc5924c0 to your computer and use it in GitHub Desktop.
Save ivey/18dfeced31f3cc5924c0 to your computer and use it in GitHub Desktop.
Pretty print middleware for Go
import "net/http/httptest"
func (a *IndentJSONMiddleware) ServeHTTP(w http.ResponseWriter, r *http.Request) {
rec := httptest.NewRecorder()
a.handler.ServeHTTP(rec, r)
for k, v := range rec.Header() {
w.Header()[k] = v
}
var pretty bytes.Buffer
json.Indent(&pretty, rec.Body.Bytes(), "", " ")
r.Header.Set("Content-Length", strconv.Itoa(pretty.Len()))
w.Header().Set("Content-Type", "application/json")
w.Write(pretty.Bytes())
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment