From 472d17509315e5d448c7e85787bbfa9c3d55c575 Mon Sep 17 00:00:00 2001 From: Sky Johnson Date: Tue, 1 Apr 2025 13:26:14 -0500 Subject: [PATCH] modify default content type --- core/http/Server.go | 33 +++++++++++++++++++++++++-------- 1 file changed, 25 insertions(+), 8 deletions(-) diff --git a/core/http/Server.go b/core/http/Server.go index e70ecb9..d510a83 100644 --- a/core/http/Server.go +++ b/core/http/Server.go @@ -3,6 +3,7 @@ package http import ( "context" "encoding/json" + "fmt" // Added for fmt.Fprintf "net" "net/http" "time" @@ -279,21 +280,37 @@ func writeResponse(w http.ResponseWriter, result any) { result = httpResp.Body // Set result to body for processing below } - switch res := result.(type) { - case string: - // String result - default to plain text - setContentTypeIfMissing(w, contentTypePlain) - w.Write([]byte(res)) - default: - // All other types - convert to JSON + // Check if it's a map (table) or array - return as JSON + isJSON := false + + switch result.(type) { + case map[string]any, []any, []float64, []string, []int: + isJSON = true + } + + if isJSON { setContentTypeIfMissing(w, contentTypeJSON) - data, err := json.Marshal(res) + data, err := json.Marshal(result) if err != nil { logger.Error("Failed to marshal response: %v", err) http.Error(w, "Internal Server Error", http.StatusInternalServerError) return } w.Write(data) + return + } + + // All other types - convert to plain text + setContentTypeIfMissing(w, contentTypePlain) + + switch r := result.(type) { + case string: + w.Write([]byte(r)) + case []byte: + w.Write(r) + default: + // Convert any other type to string + fmt.Fprintf(w, "%v", r) } }