shad-go/coverme/utils/httputils.go

33 lines
620 B
Go
Raw Normal View History

2020-03-19 21:57:07 +00:00
// +build !change
package utils
import (
"encoding/json"
"net/http"
)
func RespondJSON(w http.ResponseWriter, status int, data interface{}) error {
response, err := json.Marshal(data)
if err != nil {
return err
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(status)
_, _ = w.Write(response)
return nil
}
func ServerError(w http.ResponseWriter) {
w.WriteHeader(http.StatusInternalServerError)
_, _ = w.Write([]byte("Server encountered an error."))
}
func BadRequest(w http.ResponseWriter, message string) {
w.WriteHeader(http.StatusBadRequest)
_, _ = w.Write([]byte(message))
}