WriteYAMLResponse writes some YAML as a HTTP response.
(w http.ResponseWriter, v interface{})
| 61 | |
| 62 | // WriteYAMLResponse writes some YAML as a HTTP response. |
| 63 | func WriteYAMLResponse(w http.ResponseWriter, v interface{}) { |
| 64 | // There is not standardised content-type for YAML, text/plain ensures the |
| 65 | // YAML is displayed in the browser instead of offered as a download |
| 66 | w.Header().Set("Content-Type", "text/plain; charset=utf-8") |
| 67 | |
| 68 | data, err := yaml.Marshal(v) |
| 69 | if err != nil { |
| 70 | http.Error(w, err.Error(), http.StatusInternalServerError) |
| 71 | return |
| 72 | } |
| 73 | |
| 74 | // We ignore errors here, because we cannot do anything about them. |
| 75 | // Write will trigger sending Status code, so we cannot send a different status code afterwards. |
| 76 | // Also this isn't internal error, but error communicating with client. |
| 77 | _, _ = w.Write(data) |
| 78 | } |
| 79 | |
| 80 | // Sends message as text/plain response with 200 status code. |
| 81 | func WriteTextResponse(w http.ResponseWriter, message string) { |