Skip to content

Commit e8d4a65

Browse files
author
Derek Dowling
committed
Properly setting content length for json responses
1 parent aa388db commit e8d4a65

File tree

1 file changed

+10
-1
lines changed

1 file changed

+10
-1
lines changed

response.go

+10-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"encoding/json"
55
"fmt"
66
"net/http"
7+
"strconv"
78
)
89

910
type DataResponse struct {
@@ -57,9 +58,17 @@ func RespondWithErrors(w http.ResponseWriter, errors []*Error) error {
5758

5859
// Respond formats a JSON response with the appropriate headers.
5960
func respond(w http.ResponseWriter, status int, payload interface{}) error {
61+
content, err := json.MarshalIndent(payload, "", " ")
62+
if err != nil {
63+
return err
64+
}
65+
6066
w.Header().Add("Content-Type", ContentType)
67+
w.Header().Set("Content-Length", strconv.Itoa(len(content)))
6168
w.WriteHeader(status)
62-
return json.NewEncoder(w).Encode(payload)
69+
w.Write(content)
70+
71+
return nil
6372
}
6473

6574
func prepareError(error *Error) *ErrorResponse {

0 commit comments

Comments
 (0)