refactor(webui): decode JSON request bodies with generic decodeBody

This commit is contained in:
sam committed 2026-10-10 11:05:58 +11:00
1 parent 3f76f15daa
commit 2aacac2495
7 files changed
+35 -40

No files matched your search

+11
View File
@@ -432,3 +432,14 @@ func writeJSON(w http.ResponseWriter, v any) {
w.Header().Set("Content-Type", "application/json")
_ = json.NewEncoder(w).Encode(v)
}
// decodeBody decodes the JSON request body into a T, answering 400 itself
// when the body is invalid.
func decodeBody[T any](w http.ResponseWriter, r *http.Request) (T, bool) {
var v T
if err := json.NewDecoder(r.Body).Decode(&v); err != nil {
http.Error(w, "invalid request body: "+err.Error(), http.StatusBadRequest)
return v, false
}
return v, true
}