WriteJSONResponse writes some JSON as a HTTP response.
(w http.ResponseWriter, v any)
| 49 | |
| 50 | // WriteJSONResponse writes some JSON as a HTTP response. |
| 51 | func WriteJSONResponse(w http.ResponseWriter, v any) { |
| 52 | w.Header().Set("Content-Type", "application/json") |
| 53 | |
| 54 | data, err := json.Marshal(v) |
| 55 | if err != nil { |
| 56 | http.Error(w, err.Error(), http.StatusInternalServerError) |
| 57 | return |
| 58 | } |
| 59 | |
| 60 | // We ignore errors here, because we cannot do anything about them. |
| 61 | // Write will trigger sending Status code, so we cannot send a different status code afterwards. |
| 62 | // Also this isn't internal error, but error communicating with client. |
| 63 | _, _ = w.Write(data) |
| 64 | } |
| 65 | |
| 66 | // WriteYAMLResponse writes some YAML as a HTTP response. |
| 67 | func WriteYAMLResponse(w http.ResponseWriter, v any) { |
no test coverage detected