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