Skip to main content
Every handler in Mizu uses a *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:
  1. Status code - HTTP status like 200, 404, or 500
  2. Headers - Metadata like Content-Type and Cache-Control
  3. Body - The actual data sent to the client
Mizu’s response helpers handle all three. You typically just call one method and return.

Text responses

Send plain text with c.Text():
This sets:
  • Status: 200
  • Content-Type: text/plain; charset=utf-8
  • Body: β€œHello, world!”
If the string isn’t valid UTF-8, it’s sent as application/octet-stream.

JSON responses

Send structured data with c.JSON():
This sets:
  • Status: 200
  • Content-Type: application/json; charset=utf-8
  • Body: JSON-encoded data

HTML responses

Send HTML content with c.HTML():
For templates, use Go’s html/template:

Setting status codes

Each response method takes a status code as the first argument:
If you pass 0, Mizu uses the previously set status (default 200):
Check the current status:

Working with headers

Set headers before writing the body:
Once you write the body (via c.JSON(), c.Text(), etc.), headers are sent and can’t be changed.

Redirects

Send the client to a different URL:

Empty responses

Return no body with status 204:

Serving files

Serve a file from disk:
Force the browser to download (adds Content-Disposition header):
Both methods:
  • 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:
SSE sets these headers automatically:
  • Content-Type: text/event-stream
  • Cache-Control: no-cache
  • Connection: keep-alive
Client-side JavaScript:

Raw bytes

Send arbitrary bytes with a custom content type:

Method reference

Next steps

Request

Read input from requests.

Error Handling

Handle response errors.