From 3523f5855a563c03632fc1ca302ae7d304b67ee7 Mon Sep 17 00:00:00 2001 From: liwei Date: Wed, 8 Feb 2023 14:59:59 +0800 Subject: [PATCH] fix: api ok nil response --- pkg/rest/util/util.go | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/pkg/rest/util/util.go b/pkg/rest/util/util.go index b9dbecfd..ccf4b017 100644 --- a/pkg/rest/util/util.go +++ b/pkg/rest/util/util.go @@ -15,13 +15,15 @@ func ReadJson(w http.ResponseWriter, r *http.Request, v any) bool { } func WriteJsonOk(w http.ResponseWriter, v any) { - WriteJson(w, http.StatusOK, v) + if v == nil { + WriteJson(w, http.StatusOK, model.NewResultWithData[any](nil)) + } else { + WriteJson(w, http.StatusOK, v) + } } func WriteJson(w http.ResponseWriter, code int, v any) { w.Header().Set("Content-Type", "application/json; charset=utf-8") w.WriteHeader(code) - if v != nil { - json.NewEncoder(w).Encode(v) - } + json.NewEncoder(w).Encode(v) }