Skip to content

Commit

Permalink
redo detect content type on respond
Browse files Browse the repository at this point in the history
  • Loading branch information
dbubel committed Oct 28, 2021
1 parent a69a15a commit c969db8
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
7 changes: 7 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,10 @@ require (
golang.org/x/sys v0.0.0-20210923061019-b8560ed6a9b7 // indirect
gopkg.in/go-playground/assert.v1 v1.2.1 // indirect
)

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/go-playground/locales v0.14.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c // indirect
)
9 changes: 7 additions & 2 deletions responder.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ func RespondJSON(w http.ResponseWriter, r *http.Request, code int, data interfac
})
return Respond(w, r, http.StatusInternalServerError, resp)
}
w.Header().Set("Content-Type", "application/json")
return Respond(w, r, code, jsonData)
}

Expand All @@ -31,8 +32,12 @@ func Respond(w http.ResponseWriter, r *http.Request, code int, data []byte) (int
if err != nil {
return -1, err
}
contentType := http.DetectContentType(data)
w.Header().Set("Content-Type", contentType)

// content type is not set so attempt to set it
if w.Header().Get("Content-Type") == "" {
w.Header().Set("Content-Type", http.DetectContentType(data))
}

w.WriteHeader(code)
return w.Write(data)
}

0 comments on commit c969db8

Please sign in to comment.