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 onCtx that make handlers easy to read and maintain.
Code
Create a file namedmain.go and add this code:
Run
Start your server with:-
Visit http://localhost:8080/search?q=mizu
Response:
You searched for: mizu - Send a form using curl:
Form submitted by Alice (alice@example.com)
- Visit http://localhost:8080/hello/Bob
Response:
Hello, Bob
How it works
Mizu’sCtx helpers wrap parts of Go’s http.Request to make input handling simpler.
| Method | Reads from | Example |
|---|---|---|
c.Query("key") | Query string | /search?q=mizu |
c.Form() | Form body | name=Alice&email=alice@example.com |
c.Param("name") | Path parameters | /hello/Alice with /hello/{name} |
Queryreads values from the URL.Formparses key-value pairs from POST data.Paramextracts variables from the route path.
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:
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.