Repository navigation
Request
hime.Context embeds *http.Request, so every standard request method is
available. On top of that it adds typed value helpers that trim surrounding
spaces (and strip , thousands separators) before parsing.
Three parallel families read values:
-
FormValue*— query string and request body (same source ashttp.Request.FormValue) -
PostFormValue*— request body only -
QueryValue*— query string only
page := ctx.QueryValueInt("page") // /list?page=2 -> 2
name := ctx.PostFormValueTrimSpace("name") // trims surrounding spaces
qty := ctx.FormValueInt("qty") // "1,234" -> 1234Each family provides TrimSpace, TrimSpaceComma, Int, Int64, Float32,
and Float64.
For repeated fields (checkboxes, multi-select, repeated query params) use the slice getters:
tags := ctx.QueryValues("tag") // /?tag=go&tag=web -> [go web]
ids := ctx.PostFormValues("id")
all := ctx.FormValues("id") // query + bodyvar body struct {
Name string `json:"name"`
}
err := ctx.BindJSON(&body) // or ctx.BindXML(&body)file, header, err := ctx.FormFileNotEmpty("avatar") // errors on an empty upload
header, err := ctx.FormFileHeader("avatar") // header without opening the filectx.AddCookie("session", token, &hime.CookieOptions{
Path: "/",
HttpOnly: true,
Secure: true,
SameSite: http.SameSiteLaxMode,
})
v := ctx.CookieValue("session")
ctx.DelCookie("session", nil)Signed cookies are tamper-evident: the value is signed so a client can read it
but can not change it without detection. Set a CookieSigner on the app to
enable them — hime.NewHMACCookieSigner is a built-in HMAC-SHA256 signer, or
implement CookieSigner yourself.
app.CookieSigner = hime.NewHMACCookieSigner(secretKey) // 32+ random bytesThen use the signed counterparts of AddCookie / CookieValue:
ctx.AddSignedCookie("session", userID, &hime.CookieOptions{
Path: "/",
HttpOnly: true,
Secure: true,
SameSite: http.SameSiteLaxMode,
})
userID := ctx.SignedCookieValue("session") // "" if absent OR tamperedNotes:
- The cookie name is bound into the signature, so a signed value can not be moved to a different cookie name. Verification is constant time.
-
SignedCookieValuereturns""for a missing cookie and a tampered one alike (don't trust it either way). To tell them apart, read the raw value withCookieValueand call the signer'sVerifyyourself. - It signs but does not encrypt — the value stays readable by the client.
Serve over HTTPS with
Secure+HttpOnly, and useMaxAgefor expiry (the signature itself never expires). -
AddSignedCookie/SignedCookieValuepanic ifapp.CookieSigneris unset, andNewHMACCookieSignerpanics on an empty key.
ctx.FormState() captures the submitted values and lets you attach per-field
validation errors, so a failed form re-renders with the user's input instead of
making them retype.
func signup(ctx *hime.Context) error {
form := ctx.FormState()
if !strings.Contains(form.Value("email"), "@") {
form.AddError("email", "Enter a valid email")
}
if form.HasErrors() {
return ctx.View("signup", map[string]any{"Form": form})
}
// ... create the account ...
return ctx.Redirect("/welcome")
}<input name="email" value="{{.Form.Value "email"}}">
{{if .Form.HasError "email"}}<span class="err">{{.Form.Error "email"}}</span>{{end}}Values: Value, Values, SetValue (pre-fill an edit form). Errors:
AddError, Error (first message), Errors (all), HasError, HasErrors.