responses.go 426 B

12345678910111213141516171819202122
  1. package api
  2. import (
  3. "encoding/json"
  4. "net/http"
  5. )
  6. /*
  7. Msg writes a response with a message as json
  8. */
  9. func Msg(w http.ResponseWriter, code int, message string) {
  10. msg, err := json.Marshal(struct {
  11. Message string `json:"message"`
  12. }{message})
  13. if err != nil {
  14. http.Error(w, err.Error(), http.StatusInternalServerError)
  15. return
  16. }
  17. w.Header().Add("Content-Type", "application/json")
  18. w.WriteHeader(code)
  19. w.Write(msg)
  20. }