shad-go/coverme/utils/httputils.go
2020-03-20 00:57:07 +03:00

32 lines
620 B
Go

// +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))
}