How to build a query string
Short answer
A query string is the part of a URL after the question mark, made of key=value pairs joined by ampersands. Build it by pairing each key with its value, encoding anything unsafe, and separating pairs with &.
The shape of a query string
Everything after the ? is the query string. Each parameter is a key=value pair, and & separates them. The page or server reads these pairs to receive options like a search term, a page number, or a filter.
?q=fish&page=2Encode the values
Values can contain characters that have meaning in a URL, like spaces, &, or =. Those must be percent-encoded so they are read as data, not structure. See What is URL encoding?.
Repeated keys and order
- Repeating a key, like
tag=a&tag=b, is the usual way to pass a list - Order is generally preserved but should not be relied on for meaning
- Empty values are allowed, as in
q=