Files
kjol/go/httputil/respond.go

28 lines
693 B
Go

package httputil
import (
"encoding/gob"
"encoding/json"
"net/http"
)
func RespondJSON(w http.ResponseWriter, statusCode int, data any) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(statusCode)
json.NewEncoder(w).Encode(data)
}
func RespondGob(w http.ResponseWriter, statusCode int, data any) {
w.Header().Set("Content-Type", "application/gob")
w.WriteHeader(statusCode)
gob.NewEncoder(w).Encode(data)
}
func RespondError(w http.ResponseWriter, statusCode int, message string) {
RespondJSON(w, statusCode, map[string]string{"error": message})
}
func RespondSuccess(w http.ResponseWriter) {
RespondJSON(w, http.StatusOK, map[string]bool{"success": true})
}