22 lines
457 B
Go
22 lines
457 B
Go
package httpx
|
|
|
|
import (
|
|
"encoding/json"
|
|
"net/http"
|
|
)
|
|
|
|
func WriteJSON(w http.ResponseWriter, status int, v any) {
|
|
w.Header().Set("Content-Type", "application/json")
|
|
w.WriteHeader(status)
|
|
_ = json.NewEncoder(w).Encode(v)
|
|
}
|
|
|
|
func Error(w http.ResponseWriter, status int, msg string) {
|
|
WriteJSON(w, status, map[string]any{"ok": false, "error": msg})
|
|
}
|
|
|
|
func ReadJSON(r *http.Request, v any) error {
|
|
dec := json.NewDecoder(r.Body)
|
|
return dec.Decode(v)
|
|
}
|