RejectedSoftware Forums

Sign up

HttpServerRequest GET parameters

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?

Re: HttpServerRequest GET parameters

On 2/21/18 5:14 PM, Craig Dillabaugh wrote:

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?

See here:

http://vibed.org/api/vibe.http.server/HTTPServerRequest

form - Contains the parsed parameters of a HTML POST form request.
query - Contains all form fields supplied using the query string.

Use query, not form.

-Steve

Re: HttpServerRequest GET parameters

On Wed, 21 Feb 2018 17:30:34 -0500, Steven Schveighoffer wrote:

On 2/21/18 5:14 PM, Craig Dillabaugh wrote:

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?

See here:

http://vibed.org/api/vibe.http.server/HTTPServerRequest

form - Contains the parsed parameters of a HTML POST form request.
query - Contains all form fields supplied using the query string.

Use query, not form.

-Steve

Thank you, that does the trick.