Created
August 26, 2015 21:53
-
-
Save ivey/18dfeced31f3cc5924c0 to your computer and use it in GitHub Desktop.
Pretty print middleware for Go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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