mirror of
https://github.com/anotherhadi/iknowyou.git
synced 2026-04-11 16:37:25 +02:00
19 lines
460 B
Go
19 lines
460 B
Go
package respond
|
|
|
|
import (
|
|
"encoding/json"
|
|
"net/http"
|
|
)
|
|
|
|
// JSON writes a JSON body with the given status code.
|
|
func JSON(w http.ResponseWriter, status int, payload any) {
|
|
w.Header().Set("Content-Type", "application/json")
|
|
w.WriteHeader(status)
|
|
_ = json.NewEncoder(w).Encode(payload)
|
|
}
|
|
|
|
// Error writes a JSON error body: {"error": "message"}.
|
|
func Error(w http.ResponseWriter, status int, msg string) {
|
|
JSON(w, status, map[string]string{"error": msg})
|
|
}
|