Is a url query parameter valid if it has no value?

前端 未结 8 1206
滥情空心
滥情空心 2020-11-28 05:41

Is a url like http://example.com/foo?bar valid?

I\'m looking for a link to something official that says one way or the other. A simple yes/no answer or

8条回答
  •  伪装坚强ぢ
    2020-11-28 05:54

    URI Spec

    The only relevant part of the URI spec is to know everything between the first ? and the first # fits the spec's definition of a query. It can include any characters such as [:/.?]. This means that a query string such as ?bar, or ?ten+green+apples is valid.

    Find the RFC 3986 here

    HTML Spec

    isindex is not meaningfully HTML5.

    It's provided deprecated for use as the first element in a form only, and submits without a name.

    If the entry's name is "isindex", its type is "text", and this is the first entry in the form data set, then append the value to result and skip the rest of the substeps for this entry, moving on to the next entry, if any, or the next step in the overall algorithm otherwise.

    The isindex flag is for legacy use only. Forms in conforming HTML documents will not generate payloads that need to be decoded with this flag set.

    The last time isindex was supported was HTML3. It's use in HTML5 is to provide easier backwards compatibility.

    Support in libraries

    Support in libraries for this format of URI varies however some libraries do provide legacy support to ease use of isindex.

    Perl URI.pm (special support)

    Some libraries like Perl's URI provide methods of parsing these kind of structures

    $uri->query_keywords
    $uri->query_keywords( $keywords, ... )
    $uri->query_keywords( \@keywords )
    Sets and returns query components that use the keywords separated by "+" format.
    

    Node.js url (no special support)

    As another far more frequent example, node.js takes the normal route and eases parsing as either

    • A string
    • or, an object of keys and values (using parseQueryString)

    Most other URI-parsing APIs following something similar to this.

    • PHP parse_url, follows as similar implementation but only returns the string for the query. Parsing into an object of k=>v requires parse_string()

提交回复
热议问题