Skip to main content
Every handler in Mizu uses a *mizu.Ctx to send responses. You can return text, JSON, HTML, files, or streams easily without working directly with http.ResponseWriter.

Set status codes

You can set a status code before writing the response body.
c.Status(202)
return c.Text(0, "Accepted")
If you pass 0 as the status code, Mizu uses the one already set. You can check it anytime.
code := c.StatusCode()
c.Logger().Info("status", "code", code)

Send text

Send a plain text response.
return c.Text(200, "Hello, world")
If the text is not valid UTF-8, it will be written as binary data.

Send JSON

Send structured data as JSON.
return c.JSON(200, map[string]any{
	"message": "ok",
	"id":      1,
})
The content type is automatically set to application/json; charset=utf-8. If you pass 0, Mizu keeps the current status code.

Send HTML

Send HTML content.
return c.HTML(200, "<h1>Welcome</h1>")
The response will include Content-Type: text/html; charset=utf-8.

Work with headers

You can set or check headers directly.
c.Header().Set("Cache-Control", "no-store")
c.HeaderIfNone("Content-Type", "text/plain")
Set headers before writing the body so they are included in the response.

Redirect

Redirect the client to another page.
return c.Redirect(302, "/login")
If you pass 0, the default is 302 Found.

No content

Send an empty response with status code 204.
return c.NoContent()

Serve files and downloads

Serve files directly from disk.
return c.File("./public/logo.png")
Send files as downloads.
return c.Download("./report.csv", "report.csv")
Mizu uses standard Go file serving, so range requests and caching work automatically.

Stream data

Send data gradually as it becomes available.
return c.Stream(func(w io.Writer) error {
	for i := 0; i < 3; i++ {
		fmt.Fprintf(w, "chunk %d\n", i)
	}
	return nil
})

Server-Sent Events

Send live updates to the client using Server-Sent Events.
ch := make(chan any)
go func() {
	for i := 0; i < 3; i++ {
		ch <- map[string]int{"count": i}
	}
	close(ch)
}()
return c.SSE(ch)

Low-level control

For more control, you can access lower-level response features.
_ = c.Flush()
conn, rw, _ := c.Hijack()
_ = c.SetWriteDeadline(time.Now().Add(5 * time.Second))
_ = c.EnableFullDuplex()
These allow you to handle websockets, raw connections, or long-lived responses using normal Go behavior.