16 lines
400 B
Go
16 lines
400 B
Go
package utils
|
|
|
|
import (
|
|
"encoding/json"
|
|
"net/http"
|
|
)
|
|
|
|
func RespondWithJSON(w http.ResponseWriter, statusCode int, data interface{}) {
|
|
w.Header().Set("Content-Type", "application/json")
|
|
w.WriteHeader(statusCode)
|
|
json.NewEncoder(w).Encode(data)
|
|
}
|
|
|
|
func RespondWithError(w http.ResponseWriter, statusCode int, message string) {
|
|
RespondWithJSON(w, statusCode, map[string]string{"error": message})
|
|
} |