Query strings carry search terms, filters, page numbers and tracking tags in almost every URL you see. Their format looks simple, but several of its corners behave differently from one parser to the next. Every example below was produced by running the code.
The anatomy
Everything after the first ? and before any # is the query. By convention it is a list of key=value pairs joined by &. Characters that have a job in this syntax, such as &, = and #, must be percent-encoded when they are part of a value.
How a parser reads it
Here is q=caf%C3%A9+au+lait&tag=a&tag=b&page=&debug parsed with JavaScript's URLSearchParams:
| Key | Value |
|---|---|
| q | café au lait |
| tag | a |
| tag | b |
| page | (empty string) |
| debug | (empty string) |
+became a space, because query strings follow form-encoding rules.tagappears twice:getAll("tag")returns["a","b"], whileget("tag")returns only"a".page=and a baredebugboth come back as empty strings.
Parsers disagree
- Python's parse_qs drops empty values by default. On
a=1&a=2&b=it returns{'a': ['1', '2']}, with nob. Passkeep_blank_values=Trueto keep it. - Array syntax is a convention, not a standard.
tag[]=a&tag[]=bis read byURLSearchParamsas two keys literally namedtag[]. Frameworks such as PHP turn it into an array; others do not. - Repeated keys are kept as a list by some frameworks, while others keep only the first or the last value.
Building one safely
Do not join strings by hand. Let a builder encode each value:
new URLSearchParams({ q: "Tom & Jerry", page: "2" }).toString()
// "q=Tom+%26+Jerry&page=2"The ampersand inside the value became %26 and the spaces became +. If the receiver expects %20 rather than +, encode each value with encodeURIComponent instead; the function comparison shows how each language differs.
Length
The URL standard sets no maximum. RFC 9110 recommends that senders and recipients support URIs of at least 8000 octets, but browsers, servers and proxies apply their own limits, and a server may answer 414 URI Too Long. Send large data in the request body.
To inspect or edit a real query string, paste it into the Query String Parser, which lists every pair, keeps duplicates, and copies the result back as a string or as JSON.