In the World Wide Web, a query string is the part of a uniform resource locator (URL) containing data that does not fit conveniently into a hierarchical path structure. The query string commonly includes fields added to a base URI by a Web browser or other client application, for example as part of an HTML form. Query strings typically contain ? and % characters.
A web server can handle a Hypertext Transfer Protocol request either by reading a file from its file system based on the URL path or by handling the request using logic that is specific to the type of resource. In the case that special logic is invoked the query string will be available to that logic for use in its processing, along with the path component of the URL.
A typical URL containing a query string is as follows:
When a server receives a request for such a page, it may run a program, passing the query_string unchanged to the program. The first question mark is used as a separator and is not part of the query string.
A link in a web page may have a URL that contains a query string, while HTML additionally defines three ways a user agent can generate the query string:
an HTML form via the <form>…</form> element
a server-side image map via the ismap attribute on the <img> element with a <a><img ismap></a> construction
an indexed search via the now deprecated <isindex> element
The main use of query strings is to contain the content of an HTML form, also known as web form. In particular, when a form containing the fields field1, field2, field3 is submitted, the content of the fields is encoded as a query string as follows:
The query string is composed of a series of field-value pairs.
Within each pair, the field name and value are separated by an equals sign, ‘=’.
The series of pairs is separated by the ampersand, ‘&’ (or semicolon, ‘;’ for URLs embedded in HTML and not generated by a <form>…</form>; see below).
While there is no definitive standard, most web frameworks allow multiple values to be associated with a single field.
For each field of the form, the query string contains a pair field=value. Web forms may include fields that are not visible to the user; these fields are included in the query string when the form is submitted
This convention is a W3C recommendation. W3C recommends that all web servers support semicolon separators in addition to ampersand separators to allow application/x-www-form-urlencoded query strings in URLs within HTML documents without having to entity escape ampersands.
The form content is only encoded in the URI’s query string when the form submission method is GET. The same encoding is used by default when the submission method is POST, but the result is submitted as the HTTP request body rather than being included in a modified URL.