Skip to main content
Every handler in Mizu receives a *mizu.Ctx - short for context. This is your primary interface for working with HTTP requests and responses. Understanding Ctx is essential for building any Mizu application.

What is Context?

In web development, context refers to all the information associated with a single HTTP request. When a user visits your website:
  1. Their browser sends a request (URL, headers, body)
  2. Your server processes it and creates a response (status, headers, body)
  3. Various metadata is tracked (timing, request ID, user info)
Mizu wraps all of this into a single Ctx object, giving you easy access to everything you need.
The Ctx type is a thin wrapper around Go’s standard http.Request and http.ResponseWriter. It adds convenience methods while maintaining full compatibility with the standard library.

The Mizu Ctx Wrapper

When a request arrives, Mizu creates a Ctx that contains:

Creating Your First Handler with Context

Visit http://localhost:3000/hello?name=Mizu and you’ll see “Hello, Mizu!”.

Accessing the Request

The request contains everything the client sent. Use c.Request() to access the underlying *http.Request:

Common Request Properties

Writing Responses

The response writer is how you send data back to the client. While Mizu provides convenient helper methods, you can also access the raw writer:
Once you call w.WriteHeader() or w.Write(), headers are sent to the client and cannot be modified. Always set headers before writing the body.
Mizu provides helper methods that handle common response patterns:

The Request Logger

Each request has its own logger that automatically includes request context:

Working with Go’s context.Context

Every request includes a context.Context that signals when the request should stop. This is essential for:
  • Timeouts: Stop processing if it takes too long
  • Cancellation: Stop if the client disconnects
  • Value Passing: Store request-scoped data

Checking for Cancellation

Passing Context to Database Calls

Always pass the request context to database and external service calls:

Working with Headers

Headers provide metadata about requests and responses:

Reading Request Headers

Setting Response Headers

Storing Values in Context

Sometimes you need to pass data between middleware and handlers. Use context values:

Setting Values in Middleware

Reading Values in Handlers

Summary

Next steps

Request

Learn more about reading request data.

Response

All the ways to send responses.