I am trying to write an app using the REST API but can't figure out how to access the parameters in the request GET. I have the following code:
void main()
{
auto router = new URLRouter;
router.get("/basicreport", &getBasicReport);
auto settings = new HTTPServerSettings;
settings.port = 8080;
settings.bindAddresses = ["::1", "127.0.0.1"];
listenHTTP(settings, router);
logInfo("Please open http://127.0.0.1:8080/ in your browser.");
runApplication();
}
void getBasicReport(HTTPServerRequest req, HTTPServerResponse res)
{
import std.conv;
string form_year = req.form.get("year");
string form_budget = req.form.get("budget_id");
URL url = req.fullURL();
writeln("URL path : " ~ to!string(url.path));
writeln("Query string : " ~ to!string(url.queryString));
writeln("Form year: " ~ form_year ~ " budget " ~ to!string(form_budget));
render!("basicreport.dt", form_budget, form_year)(res);
}
If I start my server and and execute the request:http://127.0.0.1:8080/basicreport?year=2018&budget_id=7
The following is printed by the server.
URL path : /basicreport
Query string : year=2018&budget_id=7
Form year: budget
The page renders fine (basicreport.dt) but the year
and budget_id
variables are empty (as you can see when I try to dump them out). Any ideas what am I doing wrong?