Files
api-golang/internal/handlers/response.go

27 lines
779 B
Go

package handlers
import (
"encoding/json"
"net/http"
)
// jsonError writes a JSON error response with the given status code and message.
func jsonError(w http.ResponseWriter, status int, msg string) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(status)
_ = json.NewEncoder(w).Encode(map[string]string{"error": msg})
}
// jsonOK writes a 200 JSON response with the given payload.
func jsonOK(w http.ResponseWriter, v any) {
w.Header().Set("Content-Type", "application/json")
_ = json.NewEncoder(w).Encode(v)
}
// jsonCreated writes a 201 JSON response with the given payload.
func jsonCreated(w http.ResponseWriter, v any) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusCreated)
_ = json.NewEncoder(w).Encode(v)
}