receipt_indexer/backend/cmd/api/json.go
Ethan Wellenreiter 9cd57daf4b Some basic helper functions for writing a json back to the client
Signed-off-by: Ethan Wellenreiter <ewellenreiter@gmail.com>
2025-04-30 21:00:20 -04:00

21 lines
436 B
Go

package main
import (
"encoding/json"
"net/http"
)
func writeJSON(w http.ResponseWriter, status int, data any) error {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(status)
return json.NewEncoder(w).Encode(data)
}
func writeJSONError(w http.ResponseWriter, status int, message string) error {
type envelope struct {
Error string `json:"error"`
}
return writeJSON(w, status, &envelope{Error: message})
}