Function
WriteJSON
(w http.ResponseWriter, status int, data any, headers ...http.Header)
Source from the content-addressed store, hash-verified
| 12 | } |
| 13 | |
| 14 | func WriteJSON(w http.ResponseWriter, status int, data any, headers ...http.Header) error { |
| 15 | out, err := json.Marshal(data) |
| 16 | if err != nil { |
| 17 | return err |
| 18 | } |
| 19 | |
| 20 | if len(headers) > 0 { |
| 21 | for key, value := range headers[0] { |
| 22 | w.Header()[key] = value |
| 23 | } |
| 24 | } |
| 25 | |
| 26 | w.Header().Set("Content-Type", "application/json") |
| 27 | w.WriteHeader(status) |
| 28 | _, err = w.Write(out) |
| 29 | return err |
| 30 | } |
| 31 | |
| 32 | func ErrorJSON(w http.ResponseWriter, err error, status int) { |
| 33 | payload := JSONResponse{ |
Tested by
no test coverage detected