MCPcopy
hub / github.com/fabiolb/fabio / writeJSON

Function writeJSON

admin/api/api.go:10–29  ·  view source on GitHub ↗
(w http.ResponseWriter, r *http.Request, v any)

Source from the content-addressed store, hash-verified

8)
9
10func writeJSON(w http.ResponseWriter, r *http.Request, v any) {
11 _, pretty := r.URL.Query()["pretty"]
12
13 var buf []byte
14 var err error
15 if pretty {
16 buf, err = json.MarshalIndent(v, "", " ")
17 } else {
18 buf, err = json.Marshal(v)
19 }
20
21 if err != nil {
22 log.Print("[ERROR] ", err)
23 http.Error(w, "internal error", 500)
24 return
25 }
26
27 w.Header().Set("Content-Type", "application/json; charset=utf-8")
28 w.Write(buf)
29}

Callers 4

ServeHTTPMethod · 0.85
ServeHTTPMethod · 0.85
ServeHTTPMethod · 0.85
ServeHTTPMethod · 0.85

Calls 4

ErrorMethod · 0.80
SetMethod · 0.45
HeaderMethod · 0.45
WriteMethod · 0.45

Tested by

no test coverage detected