Write sets the Content-Type header field to indicate JSON data, writes the header using status, then writes v to w. It logs any error encountered during the write.
(ctx context.Context, w http.ResponseWriter, status int, v interface{})
| 37 | // then writes v to w. |
| 38 | // It logs any error encountered during the write. |
| 39 | func Write(ctx context.Context, w http.ResponseWriter, status int, v interface{}) { |
| 40 | w.Header().Set("Content-Type", "application/json; charset=utf-8") |
| 41 | w.WriteHeader(status) |
| 42 | |
| 43 | err := json.NewEncoder(w).Encode(Array(v)) |
| 44 | if err != nil { |
| 45 | log.Error(ctx, err) |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | // Array returns an empty JSON array if v is a nil slice, |
| 50 | // so that it renders as "[]" rather than "null". |