Cgi.request
T request(T = string)(in string name, in T def = T.init) const nothrowGets 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.
*/
int a = cgi.request("number", 10);
if(cgi.get["number"] == "11")
assert(a == 11); // conversion succeeds
if("number" !in cgi.get)
assert(a == 10); // no value means you can't convert - give the default
if(cgi.get["number"] == "twelve")
assert(a == 10); // conversion from string to int would fail, so we get the defaultYou can use an enum as an easy whitelist, too:
enum Operations {
add, remove, query
}
auto op = 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