shad-go/coverme/utils/httputils.go
2022-02-10 22:06:57 +00:00

33 lines
639 B
Go

//go:build !change
// +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))
}