*mizu.Ctx to send responses. The context provides helper methods for common response types like JSON, HTML, and files, while still giving you access to the underlying http.ResponseWriter when needed.
Response basics
Each response has three parts:- Status code - HTTP status like 200, 404, or 500
- Headers - Metadata like Content-Type and Cache-Control
- Body - The actual data sent to the client
Text responses
Send plain text withc.Text():
- Status: 200
- Content-Type:
text/plain; charset=utf-8 - Body: “Hello, world!”
application/octet-stream.
JSON responses
Send structured data withc.JSON():
- Status: 200
- Content-Type:
application/json; charset=utf-8 - Body: JSON-encoded data
HTML responses
Send HTML content withc.HTML():
html/template:
Setting status codes
Each response method takes a status code as the first argument:0, Mizu uses the previously set status (default 200):
Working with headers
Set headers before writing the body:c.JSON(), c.Text(), etc.), headers are sent and can’t be changed.
Redirects
Send the client to a different URL:| Code | Meaning | When to use |
|---|---|---|
| 301 | Moved Permanently | URL changed forever |
| 302 | Found | Temporary redirect (default if you pass 0) |
| 303 | See Other | Redirect after POST |
| 307 | Temporary Redirect | Preserve method |
| 308 | Permanent Redirect | Preserve method |
Empty responses
Return no body with status 204:Serving files
Serve a file from disk:- Auto-detect Content-Type from the file extension
- Support range requests (for video seeking, resumable downloads)
- Handle If-Modified-Since caching
Streaming responses
Send data gradually as it becomes available:Server-Sent Events (SSE)
Send real-time updates to the client:Content-Type: text/event-streamCache-Control: no-cacheConnection: keep-alive
Raw bytes
Send arbitrary bytes with a custom content type:Low-level control
For advanced use cases, access the underlying response features:Method reference
| Method | Signature | Purpose |
|---|---|---|
Text | (code int, s string) error | Plain text |
JSON | (code int, v any) error | JSON data |
HTML | (code int, s string) error | HTML content |
File | (code int, path string) error | Serve file |
Download | (code int, path, name string) error | Force download |
Bytes | (code int, b []byte, ct string) error | Raw bytes |
Stream | (fn func(io.Writer) error) error | Streaming |
SSE | (ch <-chan any) error | Server-Sent Events |
Redirect | (code int, url string) error | Redirect |
NoContent | () error | 204 No Content |
Next steps
- Request - Read input from requests
- Streaming - Real-time data delivery
- Error handling - Handle response errors