Gets a request variable as a specific type, or the default value of it isn't there
or isn't convertible to the request type.
Checks both GET and POST variables, preferring the POST variable, if available.
A nice trick is using the default value to choose the type:
/*
The return value will match the type of the default.
Here, I gave 10 as a default, so the return value will
be an int.
If the user-supplied value cannot be converted to the
requested type, you will get the default value back.
*/inta = cgi.request("number", 10);
if(cgi.get["number"] == "11")
assert(a == 11); // conversion succeedsif("number" !incgi.get)
assert(a == 10); // no value means you can't convert - give the defaultif(cgi.get["number"] == "twelve")
assert(a == 10); // conversion from string to int would fail, so we get the default
You can use an enum as an easy whitelist, too:
enumOperations {
add, remove, query
}
autoop = cgi.request("op", Operations.query);
if(cgi.get["op"] == "add")
assert(op == Operations.add);
if(cgi.get["op"] == "remove")
assert(op == Operations.remove);
if(cgi.get["op"] == "query")
assert(op == Operations.query);
if(cgi.get["op"] == "random string")
assert(op == Operations.query); // the value can't be converted to the enum, so we get the default
Gets a request variable as a specific type, or the default value of it isn't there or isn't convertible to the request type.
Checks both GET and POST variables, preferring the POST variable, if available.
A nice trick is using the default value to choose the type:
You can use an enum as an easy whitelist, too: