Archived
2
0
This repository has been archived on 2024-06-24. You can view files and clone it, but cannot push or open issues or pull requests.
neko-custom/server/internal/http/response/response.go

33 lines
716 B
Go
Raw Normal View History

2020-01-13 21:05:38 +13:00
package response
import (
"encoding/json"
"net/http"
2021-10-06 09:38:24 +13:00
"m1k1o/neko/internal/http/endpoint"
2020-01-13 21:05:38 +13:00
)
// JSON encodes data to rw in JSON format. Returns a pointer to a
// HandlerError if encoding fails.
func JSON(w http.ResponseWriter, data interface{}, status int) error {
w.WriteHeader(status)
w.Header().Set("Content-Type", "application/json")
err := json.NewEncoder(w).Encode(data)
if err != nil {
return &endpoint.HandlerError{
Status: http.StatusInternalServerError,
2020-01-19 12:30:09 +13:00
Message: "unable to write JSON response",
2020-01-13 21:05:38 +13:00
Err: err,
}
}
return nil
}
// Empty merely sets the response code to NoContent (204).
func Empty(w http.ResponseWriter) error {
w.WriteHeader(http.StatusNoContent)
return nil
}