goutils/http/json.go
2025-04-24 15:58:21 +02:00

20 lines
429 B
Go

package webutils
import (
"encoding/json"
"fmt"
"net/http"
"git.mstar.dev/mstar/goutils/other"
)
func SendJson(w http.ResponseWriter, data any) error {
encoded, err := json.Marshal(data)
if err != nil {
return other.Error("webutils.SendJson", "failed to marshal data to json", err)
}
w.Header().Add("Content-Type", "application/json")
// Can't really catch here
_, _ = fmt.Fprint(w, string(encoded))
return nil
}