intermediate web-app

Query Parameters and Search

URL query parameters — the ?key=value part of a URL — let users filter and search without submitting a form. /posts?tag=tutorial shows only tagged posts. req.query() reads them all.

Part of: Frame framework

req.query() reads ?key=value from the URL, or returns empty string if absent:

	GET "/posts" :
		string tag = req.query("tag")
		string sql = ""
		string params = "[]"

		if tag != ""
			sql = "SELECT title, slug FROM posts WHERE tag = ? ORDER BY created_at DESC"
			params = "[\"" + tag + "\"]"
		else
			sql = "SELECT title, slug FROM posts ORDER BY created_at DESC"

		string result = db.query(sql, params)
GET /posts          → all posts
GET /posts?tag=tips → only posts tagged "tips"

req.query("tag") returns the ?tag= value or empty string if it is missing. Build different SQL branches based on whether the filter is set — but always use ? placeholders, never concatenate query values directly into SQL.

Show the active filter in the page so users know what they're looking at:

		string heading = "All Posts"
		if tag != ""
			heading = "Posts tagged: " + tag

		html:
			<h1>{heading}</h1>
GET /posts?tag=tips → heading reads: Posts tagged: tips

The heading variable is auto-escaped when interpolated, so if someone puts HTML in the URL, it renders as harmless text, not executable code. Always use plain interpolation (no !) for user-supplied values in output.

  • req.query("key") reads ?key=value from the URL — returns empty string if absent
  • Check if the value is not empty before deciding which SQL branch to run
  • Always use ? placeholders — never concatenate query string values into SQL
  • Plain interpolation auto-escapes user input in HTML output — safe against XSS
📚 → Book 1, Chapter 8 : JSON API Endpoints →
Copied!