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 + } +}