Query string
Encyclopedia : Q : QU : QUE : Query string
In the World Wide Web, a query string is the part of a URL that contains data to be passed to CGI programs.
The
Mozilla URL location bar showing an URL with the query string
title=Main_page&action=raw
When a web page is requested via the HyperText Transfer Protocol, the server locates a file in its file system based on the requested URL. This file may be a regular file or a program. In the second case, the server may (depending on its configuration) run the program, sending its output as the required page. The query string is a part of the URL which is passed to the program. Its use permits data to be passed from the HTTP client (often a browser) to the program which generates the web page.
Structure
A typical URL containing a query string is as follows:
http://server/path/program?query_string
When a server receives a request for such a page, it runs a program (if configured to do so), passing the
query_string unchanged to the program. The 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. However, the main use of query strings is to contain the content of a 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:
field1=value1&field2=value2&field3=value3...
- The query string is composed of a series of field-value pairs.
- The field-value pairs are each separated by an equal sign.
- The series of pairs is separated by the ampersand, '&'. (The W3C recommends that CGI programs should accept ';' as well as '&'.[link])
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.
Technically, the form content is only encoded as a 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 not sent as a query string, that is, is not added to the action URL of the form. Rather, the string is sent as the body of the request.
URL encoding
Some characters cannot be part of a URL (for example, the space) and some other characters have a special meaning in a URL: for example, the character
# is used to locate a point within a page; the character
= is used to separate a name from a value. A query string may need to be converted to satisfy these constraints. This can be done using a schema known as
URL encoding.
In particular, RFC 1738 specifies that “only alphanumerics, the special characters "$-_.+!*'(),", and reserved characters used for their reserved purposes may be used unencoded within a URL”. All characters in a query string can be replaced by their hexadecimal value precedeed by the symbol %. For example, the equal sign can be replaced by %3D. All characters can be replaced this way; for the characters that are forbidden in a query string, this is not only possible but necessary.
The space character can be also represented by +.
RFC
As defined in RFC 1738, an URL of scheme
http can contain a
searchpart following the rest of the URL and separated from it by a
? character. RFC 3986 specifies that the
query component of an
URI is the part between the
? and the end of the URI or the character
#. The term
query string is of common usage for referring to this part for the case of HTTP URLs.
Example
If a form is embedded in an
HTML page as follows: