What is a valid URL query string?

后端 未结 3 1855
醉话见心
醉话见心 2020-12-10 13:39

What characters are allowed in an URL query string?

Do query strings have to follow a particular format?

3条回答
  •  再見小時候
    2020-12-10 14:31

    Wikipedia has your answer: http://en.wikipedia.org/wiki/Query_string

    "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 # can be used to further specify a subsection (or fragment) of a document; 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, encoding the query string uses the following rules:

    • Letters (A-Z and a-z), numbers (0-9) and the characters '.','-','~' and '_' are left as-is
    • SPACE is encoded as '+' or %20[citation needed]
    • All other characters are encoded as %FF hex representation with any non-ASCII characters first encoded as UTF-8 (or other specified encoding)

    The octet corresponding to the tilde ("~") character is often encoded as "%7E" by older URI processing implementations; the "%7E" can be replaced by"~" without changing its interpretation. The encoding of SPACE as '+' and the selection of "as-is" characters distinguishes this encoding from RFC 1738."

    Regarding the format, query strings are name value pairs. The ? separates the query string from the URL. Each name value pair is separated by an ampersand (&) while the name (key) and value is separated by an equals sign (=). eg. http://domain.com?key=value&secondkey=secondvalue

    Under Structure in the Wikipedia reference I provided:

    • The question mark is used as a separator and is not part of the query string.
    • 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 ...; see below).
    • W3C recommends that all web servers support semicolon separators in addition to ampersand separators[6] to allow application/x-www-form-urlencoded query strings in URLs within HTML documents without having to entity escape ampersands.

提交回复
热议问题