What is "querystring"

The query string is the part of the URL (Uniform Resource Locator) which contains data to be transmitted to web applications such as CGI programs.

When a web page is requested through the Hypertext Transfer Protocol (HTTP), the server locates a file in your file system based on the requested URL. This file may be a common file or a program. In the second case, the server can (depending on its configuration) run the program, sending its output as the requested page. The query string is a part of the URL that is passed as a parameter to the program. Its use allows data to be sent from the HTTP client (often a web browser) to the program that generates the web page.

A typical URL containing the query string:

http://servidor/caminho/pro/programa/?query_string

When the server receives the request for such a page, it can run the program by going to query_string as a parameter for the unmodified program. The question mark is used as a separator and is not part of the query string.

A link on a web page can have a URL that contains the query string, however, HTML defines three ways a web browser can generate the query string:

  • an HTML form via the element <form>...</form>
  • a server-side image map via the attribute ismap of the element <img> by construction <a><img ismap></a>
  • an indexed search via the already obsolete element <isindex>

Of these, the main use of query strings is to capture the content of an HTML form, also known as a web form. In particular, when a form containing the fields campo1, campo2 and campo3 is submitted, the contents of the fields are encoded as a query string as follows:

campo1=valor1&campo2=valor2&campo3=valor3

The query string is composed of a field-value pair series. In each pair, the field name is separated from its value by an equal sign: =. The pair series is separated by the "commercial" (in English, ampersand): &. In some cases, the semicolon may also be used, ;, although this is not generated by forms.

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. Instead, the string is sent in the HTTP request body.