Cgi.request

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:

1 /*
2 	The return value will match the type of the default.
3 	Here, I gave 10 as a default, so the return value will
4 	be an int.
5 
6 	If the user-supplied value cannot be converted to the
7 	requested type, you will get the default value back.
8 */
9 int a = cgi.request("number", 10);
10 
11 if(cgi.get["number"] == "11")
12 	assert(a == 11); // conversion succeeds
13 
14 if("number" !in cgi.get)
15 	assert(a == 10); // no value means you can't convert - give the default
16 
17 if(cgi.get["number"] == "twelve")
18 	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:

1 enum Operations {
2 	add, remove, query
3 }
4 
5 auto op = cgi.request("op", Operations.query);
6 
7 if(cgi.get["op"] == "add")
8 	assert(op == Operations.add);
9 if(cgi.get["op"] == "remove")
10 	assert(op == Operations.remove);
11 if(cgi.get["op"] == "query")
12 	assert(op == Operations.query);
13 
14 if(cgi.get["op"] == "random string")
15 	assert(op == Operations.query); // the value can't be converted to the enum, so we get the default
class Cgi
const nothrow
T
request
(
T = string
)
(
in string name
,
in T def = T.init
)

Meta