Skip to content

Commit

Permalink
Merge pull request #57 from blackbx/responder-content-types
Browse files Browse the repository at this point in the history
JSONResponder now responds with the correct Content-Type
  • Loading branch information
alistairjudson committed Feb 5, 2021
2 parents 83a3387 + cdf684f commit 64a0e20
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 deletions response/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ type JSONResponder struct {
func (r JSONResponder) RespondWithProblem(statusCode int, detail string) {
problem := NewHTTPProblem(statusCode, detail)
r.responseWriter.WriteHeader(statusCode)
r.request.Header.Set("Content-Type", "application/problem+json")
if err := r.Encoder.Encode(problem); err != nil {
r.logger.Error("Could not respond with problem", zap.Any("value", problem))
}
Expand All @@ -46,6 +47,7 @@ func (r JSONResponder) RespondWithProblem(statusCode int, detail string) {
// Respond will take a given struct and respond with it as the body
func (r JSONResponder) Respond(statusCode int, value interface{}) {
r.responseWriter.WriteHeader(statusCode)
r.request.Header.Set("Content-Type", contentTypeForValue(value))
if err := r.Encoder.Encode(value); err != nil {
r.logger.Error("Could not respond with value", zap.Any("value", value))
}
Expand All @@ -54,9 +56,19 @@ func (r JSONResponder) Respond(statusCode int, value interface{}) {
// RespondStream will stream a response of JSON values to the client
func (r JSONResponder) RespondStream(statusCode int, valueStream <-chan interface{}) {
r.responseWriter.WriteHeader(statusCode)
r.request.Header.Set("Content-Type", "application/json")
for value := range valueStream {
if err := r.Encoder.Encode(value); err != nil {
r.logger.Error("Could not respond with value stream", zap.Any("value", value))
}
}
}

func contentTypeForValue(value interface{}) string {
switch value.(type) {
case Problem, *Problem:
return "application/problem+json"
default:
return "application/json"
}
}

0 comments on commit 64a0e20

Please sign in to comment.