Skip to main content
In this lesson, you will learn how to use cookies in Mizu to store and read small pieces of data between requests.
Cookies are a simple way to remember users, track sessions, or save preferences like language or theme. Mizu provides two helpers, Ctx.Cookie() and Ctx.SetCookie(), that make cookie handling easy.

Code

Create a file named main.go and add this code:

Run

Start the server:
Then try the following examples: Visit http://localhost:8080/set?name=Alice Response: Cookie set for Alice Your browser now stores a cookie named username. Visit http://localhost:8080/get Response: Welcome back, Alice The value comes from the cookie you set earlier. Visit http://localhost:8080/clear Response: Cookie cleared The browser cookie is now deleted.

How it works

  • Setting a cookie adds a Set-Cookie header to the HTTP response.
  • Reading a cookie checks the request headers for the matching cookie name.
  • Deleting a cookie works by sending a new cookie with an empty value and an expiration date in the past.
This approach lets you manage sessions or preferences without needing a database or external storage.

Try something new

Add a handler that counts how many times a user has visited your site:
Register it with app.Get("/visits", visitCount) and refresh the page several times. The count will increase with each visit. Cookies form the basis for session handling, login tracking, and simple user personalization. Next, continue to Middleware Basics to learn how to wrap your handlers with reusable logic like logging, timing, and authentication.