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
Click to show internal directories.
Click to hide internal directories.