Skip to main content

Query, Form, and Path

In this lesson, you will learn how to read data sent by users through query strings, form submissions, and path parameters. These are the main ways a client can send information to your server. Mizu provides simple helper methods on Ctx that make handlers easy to read and maintain.

Code

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

import (
	"log"
	"net/http"

	"github.com/go-mizu/mizu"
)

// Handle query parameters like /search?q=go
func search(c *mizu.Ctx) error {
	query := c.Query("q")
	if query == "" {
		return c.Text(http.StatusBadRequest, "Missing query parameter 'q'")
	}
	return c.Text(http.StatusOK, "You searched for: "+query)
}

// Handle form submissions from HTML forms
func submit(c *mizu.Ctx) error {
	form, err := c.Form()
	if err != nil {
		return c.Text(http.StatusBadRequest, "Invalid form data")
	}

	name := form.Get("name")
	email := form.Get("email")

	if name == "" || email == "" {
		return c.Text(http.StatusBadRequest, "Both name and email are required")
	}

	return c.Text(http.StatusOK, "Form submitted by "+name+" ("+email+")")
}

// Handle dynamic paths like /hello/alice
func greet(c *mizu.Ctx) error {
	name := c.Param("name")
	if name == "" {
		return c.Text(http.StatusBadRequest, "Name is required")
	}
	return c.Text(http.StatusOK, "Hello, "+name)
}

func main() {
	app := mizu.New()

	app.Get("/search", search)
	app.Post("/submit", submit)
	app.Get("/hello/{name}", greet)

	if err := app.Listen(":8080"); err != nil {
		log.Fatal(err)
	}
}

Run

Start your server with:
go run .
Then test the routes:
curl -X POST -d "name=Alice&email=alice@example.com" http://localhost:8080/submit
Response: Form submitted by Alice (alice@example.com)

How it works

Mizu’s Ctx helpers wrap parts of Go’s http.Request to make input handling simpler.
MethodReads fromExample
c.Query("key")Query string/search?q=mizu
c.Form()Form bodyname=Alice&email=alice@example.com
c.Param("name")Path parameters/hello/Alice with /hello/{name}
  • Query reads values from the URL.
  • Form parses key-value pairs from POST data.
  • Param extracts variables from the route path.
Each method returns safe, plain strings. If a key is missing, you get an empty string. If parsing fails, c.Form() returns an error you can check. These helpers keep your code short and close to Go’s net/http design.

Try something new

You can add a small HTML form to test the /submit route. Create a file named form.html:
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <title>Submit Example</title>
  </head>
  <body>
    <h1>Submit Form</h1>
    <form action="/submit" method="post">
      <input type="text" name="name" placeholder="Your name" required><br><br>
      <input type="email" name="email" placeholder="Your email" required><br><br>
      <button type="submit">Send</button>
    </form>
  </body>
</html>
Serve it with Mizu’s static file helper, then open it in your browser and submit the form. You will see the response from your submit handler. Now you know how to read data from query strings, forms, and path parameters in Mizu. Next, continue to Cookies to learn how to store small pieces of data between requests.