MCPcopy
hub / github.com/kagent-dev/kagent / RespondWithJSON

Function RespondWithJSON

go/core/internal/httpserver/handlers/helpers.go:27–42  ·  view source on GitHub ↗
(w http.ResponseWriter, code int, payload any)

Source from the content-addressed store, hash-verified

25}
26
27func RespondWithJSON(w http.ResponseWriter, code int, payload any) {
28 log := ctrllog.Log.WithName("http-helpers")
29
30 response, err := json.Marshal(payload)
31 if err != nil {
32 log.Error(err, "Error marshalling JSON response")
33 RespondWithError(w, http.StatusInternalServerError, "Error marshalling JSON response")
34 return
35 }
36
37 w.Header().Set("Content-Type", "application/json")
38 w.WriteHeader(code)
39 w.Write(response) //nolint:errcheck
40
41 log.V(2).Info("Sent JSON response", "statusCode", code, "responseSize", len(response))
42}
43
44func RespondWithError(w http.ResponseWriter, code int, message string) {
45 log := ctrllog.Log.WithName("http-helpers")

Calls 4

RespondWithErrorFunction · 0.85
ErrorMethod · 0.45
SetMethod · 0.45
WriteHeaderMethod · 0.45

Tested by

no test coverage detected