From 35ecaf84e01d0ef6ffd5473cc8219897425b6a65 Mon Sep 17 00:00:00 2001 From: Arthur Lu Date: Fri, 23 Oct 2026 21:55:27 +0000 Subject: [PATCH] add JSON helper to HTTP utils --- http-utils.go | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/http-utils.go b/http-utils.go index 8c4f362..604a105 100644 --- a/http-utils.go +++ b/http-utils.go @@ -2,6 +2,7 @@ package proxmoxaas_common_lib import ( "bytes" + "encoding/json" "html/template" "net/http" ) @@ -31,3 +32,16 @@ func HTTPExecuteHTMLFragmentTemplate(w http.ResponseWriter, t *template.Template return } } + +func HTTPWriteJson(w http.ResponseWriter, code int, obj any) { + w.Header().Set("Content-Type", "application/json") + json, err := json.Marshal(obj) + if err != nil { + w.WriteHeader(http.StatusInternalServerError) + return + } else { + w.WriteHeader(code) + w.Write(json) + return + } +}