27 lines
748 B
Go
27 lines
748 B
Go
// Package httpjson holds the tiny JSON response helpers shared across
|
|
// the admin, v2, and async handlers.
|
|
package httpjson
|
|
|
|
import (
|
|
"encoding/json"
|
|
"net/http"
|
|
)
|
|
|
|
// Write writes body as JSON with the given status code.
|
|
func Write(w http.ResponseWriter, status int, body any) {
|
|
w.Header().Set("Content-Type", "application/json")
|
|
w.WriteHeader(status)
|
|
_ = json.NewEncoder(w).Encode(body)
|
|
}
|
|
|
|
// Error writes a standard error JSON body with the given status code.
|
|
//
|
|
// Shape: {"error": {"type": "...", "description": "..."}}
|
|
func Error(w http.ResponseWriter, status int, description string) {
|
|
Write(w, status, map[string]any{
|
|
"error": map[string]string{
|
|
"type": http.StatusText(status),
|
|
"description": description,
|
|
},
|
|
})
|
|
}
|