Documentation
¶
Overview ¶
Package api holds the JSON, input and RFC 9457 problem-details helpers shared by every HTTP API.
Index ¶
- Constants
- func DecodeJSON(w http.ResponseWriter, r *http.Request, v any) error
- func Error(w http.ResponseWriter, r *http.Request, status int, detail string)
- func Printable(s string) bool
- func WriteJSON(w http.ResponseWriter, status int, v any)
- func WriteProblem(w http.ResponseWriter, r *http.Request, status int, detail string, ...)
Constants ¶
const MaxBodyBytes = 1 << 20
MaxBodyBytes bounds every JSON request body.
Variables ¶
This section is empty.
Functions ¶
func DecodeJSON ¶
DecodeJSON reads one JSON object into v, rejecting unknown fields, trailing data and bodies larger than MaxBodyBytes. The returned error is safe to show to the client.
func Printable ¶
Printable reports whether s is valid UTF-8 without control characters, the rule for free text that reaches storage, logs or emails.
func WriteJSON ¶
func WriteJSON(w http.ResponseWriter, status int, v any)
WriteJSON writes v with the given status.
func WriteProblem ¶
func WriteProblem(w http.ResponseWriter, r *http.Request, status int, detail string, ext map[string]any)
WriteProblem writes an RFC 9457 problem of type about:blank with the status text as title. ext adds members such as a machine-readable code; the request's trace ID is added so a client report leads to the logs.
Types ¶
This section is empty.