chirpy_server/internal/handler/response.go

20 lines
402 B
Go

package handler
import (
"encoding/json"
"log"
"net/http"
)
func JsonResponse(w http.ResponseWriter, statusCode int, payload interface{}) {
w.Header().Set("Content-Type", "application/json")
data, err := json.Marshal(payload)
if err != nil {
log.Printf("Error marshalling JSON: %v", err)
w.WriteHeader(http.StatusInternalServerError)
return
}
w.WriteHeader(statusCode)
w.Write(data)
}