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 anecdotal evidence won't cut it.
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 anecdotal evidence won't cut it.
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.
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
Node.js
url
(no special support)As another far more frequent example,
node.js
takes the normal route and eases parsing as eitherparseQueryString
)Most other URI-parsing APIs following something similar to this.
parse_url
, follows as similar implementation but only returns the string for the query. Parsing into an object ofk=>v
requiresparse_string()
"The "http" scheme is used to locate network resources via the HTTP protocol. This section defines the scheme-specific syntax and semantics for http URLs." http://www.w3.org/Protocols/rfc2616/rfc2616.html
So yes, anything is valid after a question mark. Your server may interpret differently, but anecdotally, you can see some languages treat that as a boolean value which is true if listed.
Yes, it is valid.
If one simply want to check if the parameter exists or not, this is one way to do so.
The URI RFC doesn't mandate a format for the query string. Although it is recognized that the query string will often carry name-value pairs, it is not required to (e.g. it will often contain another URI).
HTML establishes that a form submitted via HTTP GET should encode the form values as name-value pairs in the form "?key1=value1&key2=value2..." (properly encoded). Parsing of the query string is up to the server-side code (e.g. Java servlet engine).
You don't identify what server-side framework you use, if any, but it is possible that your server-side framework may assume the query string will always be in name-value pairs and it may choke on a query string that is not in that format (e.g.
?bar
). If its your own custom code parsing the query string, you simply have to ensure you handle that query string format. If its a framework, you'll need to consult your documentation or simply test it to see how it is handled.isindex
from HTML5 allows a form submission to generate such an URL, providing further evidence that it is valid for HTML. E.g.:generates an URL of type:
Standard: https://www.w3.org/TR/html5/forms.html#naming-form-controls:-the-name-attribute
isindex
is however deprecated as mentioned at: https://stackoverflow.com/a/41689431/895245As all other answers described, it's perfectly valid for checking, specially for boolean kind stuff
Here is a simple function to get the query string by name:
and now you want to check if the query string you are looking for exists or not, you may do a simple thing like:
the
exampleQueryString
will befalse
if the function can't find the query string, otherwise will betrue
.