Skip to content
Thanatat Tamtan edited this page Jun 10, 2026 · 3 revisions

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.

Form, post-form and query values

Three parallel families read values:

  • FormValue* — query string and request body (same source as http.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"  ->  1234

Each family provides TrimSpace, TrimSpaceComma, Int, Int64, Float32, and Float64.

Multiple values

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 + body

Binding the body

var body struct {
	Name string `json:"name"`
}
err := ctx.BindJSON(&body) // or ctx.BindXML(&body)

File uploads

file, header, err := ctx.FormFileNotEmpty("avatar") // errors on an empty upload
header, err := ctx.FormFileHeader("avatar")          // header without opening the file

Cookies

ctx.AddCookie("session", token, &hime.CookieOptions{
	Path:     "/",
	HttpOnly: true,
	Secure:   true,
	SameSite: http.SameSiteLaxMode,
})

v := ctx.CookieValue("session")
ctx.DelCookie("session", nil)

Signed cookies

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 bytes

Then 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 tampered

Notes:

  • 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.
  • SignedCookieValue returns "" for a missing cookie and a tampered one alike (don't trust it either way). To tell them apart, read the raw value with CookieValue and call the signer's Verify yourself.
  • It signs but does not encrypt — the value stays readable by the client. Serve over HTTPS with Secure + HttpOnly, and use MaxAge for expiry (the signature itself never expires).
  • AddSignedCookie / SignedCookieValue panic if app.CookieSigner is unset, and NewHMACCookieSigner panics on an empty key.

Form state

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.

Clone this wiki locally