query-and-forms

command
v14.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 12, 2026 License: BSD-3-Clause Imports: 2 Imported by: 0

Documentation

Overview

Query strings and form bodies: the two places where client input arrives as loose key/value pairs.

ctx.Query() is a read-only, sorted view of the URL query with typed accessors and fixed failure defaults. ctx.ReadForm decodes an urlencoded or multipart body into a struct through the `form` tag; unlike ctx.BindJSON it returns the error instead of answering the client, so a rejected form renders through errors.InvalidArgument.

Learn: read typed query values (a UUID too) with safe defaults, tell "absent" from "empty", and decode a form into a struct, rejecting a bad one with a 400.

Run:

go run main.go

Try:

curl 'http://localhost:8080/search?q=iris&page=2&tags=go&tags=web'   # 200, the parsed values
curl 'http://localhost:8080/search'                                   # 200, page 1 and sort "relevance"
curl 'http://localhost:8080/search?q=iris&shelf=11111111-1111-1111-1111-111111111111'   # 200, shelf is parsed
curl -X POST http://localhost:8080/contact \
  -d 'name=Gerasimos' -d 'email=me@example.com' -d 'message=hello' -d 'subscribe=true'   # 200, echoes the form
curl -i -X POST http://localhost:8080/contact -d 'name=Gerasimos'     # 400 envelope, message is required

Next: ../read-json-validate

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL