Skip to content

Commit

Permalink
Properly setting content length for json responses
Browse files Browse the repository at this point in the history
  • Loading branch information
Derek Dowling committed Nov 11, 2015
1 parent aa388db commit e8d4a65
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion response.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/json"
"fmt"
"net/http"
"strconv"
)

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

// Respond formats a JSON response with the appropriate headers.
func respond(w http.ResponseWriter, status int, payload interface{}) error {
content, err := json.MarshalIndent(payload, "", " ")
if err != nil {
return err
}

w.Header().Add("Content-Type", ContentType)
w.Header().Set("Content-Length", strconv.Itoa(len(content)))
w.WriteHeader(status)
return json.NewEncoder(w).Encode(payload)
w.Write(content)

return nil
}

func prepareError(error *Error) *ErrorResponse {
Expand Down

0 comments on commit e8d4a65

Please sign in to comment.