freego_api/util.go

19 lines
416 B
Go
Raw Normal View History

2022-03-03 13:28:00 -05:00
package main
import (
"encoding/json"
"net/http"
)
func respondWithError(res http.ResponseWriter, code int, message string) {
respondWithJSON(res, code, map[string]string{"error": message})
}
func respondWithJSON(res http.ResponseWriter, code int, payload interface{}) {
response, _ := json.Marshal(payload)
res.Header().Set("Content-Type", "application/json")
res.WriteHeader(code)
res.Write(response)
}