Merge "httputil: set the headers before writing them"

This commit is contained in:
Brad Fitzpatrick 2013-11-14 21:53:39 +00:00 committed by Gerrit Code Review
commit e5eade1bd6
1 changed files with 1 additions and 1 deletions

View File

@ -75,13 +75,13 @@ func ReturnJSON(rw http.ResponseWriter, data interface{}) {
func ReturnJSONCode(rw http.ResponseWriter, code int, data interface{}) {
rw.Header().Set("Content-Type", "text/javascript")
rw.WriteHeader(code)
js, err := json.MarshalIndent(data, "", " ")
if err != nil {
BadRequestError(rw, fmt.Sprintf("JSON serialization error: %v", err))
return
}
rw.Header().Set("Content-Length", strconv.Itoa(len(js)+1))
rw.WriteHeader(code)
rw.Write(js)
rw.Write([]byte("\n"))
}