Files
Crussell/backend/mw/response.go
T
popertotsandSisyphus da0e64c02b feat(mw): add content-type and response middleware
Add Content-Type enforcement middleware and generic JSON response helpers to standardize API responses.

Ultraworked with [Sisyphus](https://github.com/code-yeongyu/oh-my-openagent)

Co-authored-by: Sisyphus <clio-agent@sisyphuslabs.ai>
2026-06-24 23:42:57 +01:00

22 lines
730 B
Go

package mw
import (
"encoding/json"
"net/http"
)
// RespondJSON writes a JSON response with the given status code.
// The Content-Type is always set to application/json, overriding any
// middleware-set value, to ensure consistent JSON error responses.
func RespondJSON(w http.ResponseWriter, status int, data any) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(status)
json.NewEncoder(w).Encode(data)
}
// RespondError writes a JSON error response with the given status code and message.
// Use instead of http.Error() to ensure error responses are application/json.
func RespondError(w http.ResponseWriter, status int, message string) {
RespondJSON(w, status, map[string]string{"error": message})
}