JSONResponse attempts to set the status code, c, and marshal the given interface, d, into a response that is written to the given ResponseWriter.
(w http.ResponseWriter, d interface{}, c int)
| 11 | // JSONResponse attempts to set the status code, c, and marshal the given interface, d, into a response that |
| 12 | // is written to the given ResponseWriter. |
| 13 | func JSONResponse(w http.ResponseWriter, d interface{}, c int) { |
| 14 | dj, err := json.MarshalIndent(d, "", " ") |
| 15 | if err != nil { |
| 16 | http.Error(w, "Error creating JSON response", http.StatusInternalServerError) |
| 17 | log.Error(err) |
| 18 | } |
| 19 | w.Header().Set("Content-Type", "application/json") |
| 20 | w.WriteHeader(c) |
| 21 | fmt.Fprintf(w, "%s", dj) |
| 22 | } |
no test coverage detected