Skip to content

Commit 7306d3f

Browse files
author
Dean Karn
committed
correct headers order
1 parent baf27f4 commit 7306d3f

File tree

1 file changed

+8
-8
lines changed

1 file changed

+8
-8
lines changed

net/http/helpers.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -103,13 +103,13 @@ func ClientIP(r *http.Request) (clientIP string) {
103103

104104
// JSON marshals provided interface + returns JSON + status code
105105
func JSON(w http.ResponseWriter, status int, i interface{}) error {
106-
w.Header().Set(ContentType, ApplicationJSON)
107-
w.WriteHeader(status)
108-
enc := json.NewEncoder(w)
109-
err := enc.Encode(i)
106+
b, err := json.Marshal(i)
110107
if err != nil {
111108
return err
112109
}
110+
w.Header().Set(ContentType, ApplicationJSON)
111+
w.WriteHeader(status)
112+
_, err = w.Write(b)
113113
return nil
114114
}
115115

@@ -124,12 +124,12 @@ func JSONBytes(w http.ResponseWriter, status int, b []byte) (err error) {
124124
// JSONP sends a JSONP response with status code and uses `callback` to construct
125125
// the JSONP payload.
126126
func JSONP(w http.ResponseWriter, status int, i interface{}, callback string) error {
127-
w.Header().Set(ContentType, ApplicationJSON)
128-
w.WriteHeader(status)
129127
b, err := json.Marshal(i)
130128
if err != nil {
131129
return err
132130
}
131+
w.Header().Set(ContentType, ApplicationJSON)
132+
w.WriteHeader(status)
133133
if _, err = w.Write([]byte(callback + "(")); err == nil {
134134
if _, err = w.Write(b); err == nil {
135135
_, err = w.Write([]byte(");"))
@@ -140,12 +140,12 @@ func JSONP(w http.ResponseWriter, status int, i interface{}, callback string) er
140140

141141
// XML marshals provided interface + returns XML + status code
142142
func XML(w http.ResponseWriter, status int, i interface{}) error {
143-
w.Header().Set(ContentType, ApplicationXML)
144-
w.WriteHeader(status)
145143
b, err := xml.Marshal(i)
146144
if err != nil {
147145
return err
148146
}
147+
w.Header().Set(ContentType, ApplicationXML)
148+
w.WriteHeader(status)
149149
if _, err = w.Write(xmlHeaderBytes); err == nil {
150150
_, err = w.Write(b)
151151
}

0 commit comments

Comments
 (0)