Documentation
¶
Overview ¶
Package response provides utilities for writing HTTP responses in the Zero server. It includes functions for writing JSON responses, error responses, and setting response headers.
Index ¶
- func InternalServerError(w http.ResponseWriter, err error)
- func SetHeader(w http.ResponseWriter, key, value string)
- func StatusCode(w http.ResponseWriter) int
- func Write(w http.ResponseWriter, contentType string, statusCode int, data []byte)
- func WriteError(w http.ResponseWriter, statusCode int, err error)
- func WriteErrorMsg(w http.ResponseWriter, statusCode int, message string)
- func WriteJSON(w http.ResponseWriter, statusCode int, data any)
- type Writer
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func InternalServerError ¶
func InternalServerError(w http.ResponseWriter, err error)
InternalServerError writes a generic 500 response to the client and logs the real error server-side
func SetHeader ¶
func SetHeader(w http.ResponseWriter, key, value string)
SetHeader sets a response header with the given key and value
func StatusCode ¶
func StatusCode(w http.ResponseWriter) int
func Write ¶
func Write(w http.ResponseWriter, contentType string, statusCode int, data []byte)
Write writes a response with the given content type, status code, and data
func WriteError ¶
func WriteError(w http.ResponseWriter, statusCode int, err error)
WriteError writes an error response with the given status code and error
func WriteErrorMsg ¶
func WriteErrorMsg(w http.ResponseWriter, statusCode int, message string)
WriteErrorMsg writes an error response with the given status code and message
Types ¶
type Writer ¶
type Writer struct {
http.ResponseWriter
// contains filtered or unexported fields
}
Writer is a wrapper around http.ResponseWriter that provides a status code and a written flag It ensures that the response header is only written once
func Wrap ¶
func Wrap(w http.ResponseWriter) *Writer
Wrap wraps the given http.ResponseWriter in a Writer If the http.ResponseWriter is already a Writer, it returns it Otherwise, it creates a new Writer
func (*Writer) Unwrap ¶
func (rw *Writer) Unwrap() http.ResponseWriter
Unwrap returns the underlying http.ResponseWriter so http.NewResponseController can reach Flusher, Hijacker, etc.
func (*Writer) Write ¶
Write writes the given bytes to the response body It ensures that the response header is only written once
func (*Writer) WriteHeader ¶
WriteHeader writes the status code to the response header