add JSON helper to HTTP utils
tronnet/workflows: Static Analysis -- Golang / static-go (push) Failing after 21s
tronnet/workflows: Static Analysis -- Golang / static-go (push) Failing after 21s
This commit is contained in:
1 parent
01c1f0389a
commit
35ecaf84e0
1 file changed
+14
@@ -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
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user