BrainMinder/internal/response/json.go
2024-08-22 10:13:16 +02:00

30 lines
539 B
Go

package response
import (
"encoding/json"
"net/http"
)
func JSON(w http.ResponseWriter, status int, data any) error {
return JSONWithHeaders(w, status, data, nil)
}
func JSONWithHeaders(w http.ResponseWriter, status int, data any, headers http.Header) error {
js, err := json.MarshalIndent(data, "", "\t")
if err != nil {
return err
}
js = append(js, '\n')
for key, value := range headers {
w.Header()[key] = value
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(status)
w.Write(js)
return nil
}