RejectedSoftware Forums

Sign up

httpServerRequest params

Hello,

(entry level question ahead)

I have a simple static html page that will generate a piece of
data that I would like send back to the server. But when I try to
get the params from the request im getting an empty array. Is
the following server side code correct for this sort of thing?

//--- server code ---
import vibe.d, std.stdio;

void handleRequest(HttpServerRequest req, HttpServerResponse res)
{

res.redirect("/index.html");

}

void report(HttpServerRequest req, HttpServerResponse res) {

 writeln(req.params); // prints: [ ]

}

static this() {
auto settings = new HttpServerSettings;
settings.port = 8080;
auto router = new UrlRouter;
router.get("/", &handleRequest);
router.get("*", serveStaticFiles("./public/"));
router.get("/report", &report);
listenHttp(settings, router);
}

//--- js snippet inside the hosted HTML page ---
$.ajax({
url: 'http://localhost:8080/report',
data: 'test=42'
});

Thanks,
Josh

Re: httpServerRequest params

Am 11.09.2012 06:04, schrieb Joshua Niehus:

Hello,

(entry level question ahead)

I have a simple static html page that will generate a piece of data that
I would like send back to the server. But when I try to get the params
from the request im getting an empty array. Is the following server
side code correct for this sort of thing?

The total set of parameters is currently split up into four subsets:

query: fields in the query string of the URL
form: fields of a POSTed HTML form, if any
(application/x-www-form-urlencoded)
params: custom parameters added to the request by the server/user
json: parsed JSON of the request, if any (application/json)

The params map is used for example by the UrlRouter to put in any
matched variables in the URL (e.g. /:id/view would produce a param
'id'). You could also use this for example to store authentication
information for later processing [1].

In your example you probably also want to set the "Content-Type" header
of the ajax request to "application/x-www-form-urlencoded". In that case
the 'form' map would contain ["test": "42"].

Another alternative is to set "Content-Type" to "application/json" and
use the 'json' field to get the contents.

Regards,
Sönke

a kind of middleware, that could add something to params so that later
stages have that information available:
http://www.vibed.org/docs#http-authentication

Re: httpServerRequest params

On Tuesday, 11 September 2012 at 06:51:26 UTC, Sönke Ludwig
wrote:

Am 11.09.2012 06:04, schrieb Joshua Niehus:

Hello,

(entry level question ahead)

I have a simple static html page that will generate a piece of
data that
I would like send back to the server. But when I try to get
the params
from the request im getting an empty array. Is the following
server
side code correct for this sort of thing?

The total set of parameters is currently split up into four
subsets:

query: fields in the query string of the URL
form: fields of a POSTed HTML form, if any
(application/x-www-form-urlencoded)
params: custom parameters added to the request by the
server/user
json: parsed JSON of the request, if any (application/json)

The params map is used for example by the UrlRouter to put in
any
matched variables in the URL (e.g. /:id/view would produce a
param
'id'). You could also use this for example to store
authentication
information for later processing [1].

In your example you probably also want to set the
"Content-Type" header
of the ajax request to "application/x-www-form-urlencoded". In
that case
the 'form' map would contain ["test": "42"].

Another alternative is to set "Content-Type" to
"application/json" and
use the 'json' field to get the contents.

Regards,
Sönke

[1]: The authentication example shows how performBasicAuth() is
added as
a kind of middleware, that could add something to params so
that later
stages have that information available:
http://www.vibed.org/docs#http-authentication

Could you start adding these nice answers to the documentation or
at least the blog. this group doesn't have many members, and
answers such as this deserves more readers.

Re: httpServerRequest params

Am 11.09.2012 18:11, schrieb simendsjo:

On Tuesday, 11 September 2012 at 06:51:26 UTC, Sönke Ludwig wrote:

Am 11.09.2012 06:04, schrieb Joshua Niehus:

Hello,

(entry level question ahead)

I have a simple static html page that will generate a piece of data that
I would like send back to the server. But when I try to get the params
from the request im getting an empty array. Is the following server
side code correct for this sort of thing?

The total set of parameters is currently split up into four subsets:

query: fields in the query string of the URL
form: fields of a POSTed HTML form, if any
(application/x-www-form-urlencoded)
params: custom parameters added to the request by the server/user
json: parsed JSON of the request, if any (application/json)

The params map is used for example by the UrlRouter to put in any
matched variables in the URL (e.g. /:id/view would produce a param
'id'). You could also use this for example to store authentication
information for later processing [1].

In your example you probably also want to set the "Content-Type" header
of the ajax request to "application/x-www-form-urlencoded". In that case
the 'form' map would contain ["test": "42"].

Another alternative is to set "Content-Type" to "application/json" and
use the 'json' field to get the contents.

Regards,
Sönke

[1]: The authentication example shows how performBasicAuth() is added as
a kind of middleware, that could add something to params so that later
stages have that information available:
http://www.vibed.org/docs#http-authentication

Could you start adding these nice answers to the documentation or at
least the blog. this group doesn't have many members, and answers such
as this deserves more readers.

As soon as I have time... right now there is still too much stuff with a
higher priority (mostly non-vibe.d related).

Re: httpServerRequest params

On Tuesday, 11 September 2012 at 06:51:26 UTC, Sönke Ludwig
wrote:

The total set of parameters is currently split up into four
subsets: [...snip...]

Awesome thanks, that worked.

Re: httpServerRequest params

Hi everybody, i'm new here and i am trying to make something with vibe.d. I have a problem with tha ajax request and the form datas.

I have this in my app.d:
/*************************************************************/
void showSearch(HttpServerRequest req, HttpServerResponse res)
{

auto output = res.bodyWriter();
parseDietFile!("search.dt", req)(output);

}

void runSearch(HttpServerRequest req, HttpServerResponse res)
{

auto output = res.bodyWriter();

output.write("params<br/>");
foreach(string s; req.params)
{
	output.write(s);
}
output.write("<br/>query<br/>");
foreach(string s; req.query)
{
	output.write(s);
}

output.write("<br/>cookie<br/>");
foreach(string s; req.cookies)
{
	output.write(s);
}

output.write("<br/>form<br/>");
foreach(string s; req.form)
{
	output.write(s);
}

}

router.get("/search", &showSearch);
router.post("/runSearch", &runSearch);

And this in the template search.dt
/************************************************************/

form(id="myform", name="myform", action="/runSearch", method="post")

label vol:
select(name="returnTicket")
	option(value="false")Aller Simple
	option(value="true")Aller Retour
input(name="formSubmit", type="submit", value="Search", alt="Search")

#searchResults

script(type="text/javascript")

window.addEvent('domready', function()
{
	new Form.Request("myform", "searchResults");
});

/*************************************************************/

When I fill the form and submit it without ajax request, all is okay, the form data is written to the body of the page as well, but if i submit the form using ajax (via mootools Form.Request), i got nothing (req.form is empty). Have you any idea to help me?

This framework is cool. Keep up the good work. There is any irc channel where we can talk about vibe? Thanks.

Romain

Re: httpServerRequest params

Am 10/1/2012 11:34 AM, schrieb Romain337:

Hi everybody, i'm new here and i am trying to make something with
vibe.d. I have a problem with tha ajax request and the form datas.

I have this in my app.d:

(...)

When I fill the form and submit it without ajax request, all is okay,
the form data is written to the body of the page as well, but if i
submit the form using ajax (via mootools Form.Request), i got nothing
(req.form is empty). Have you any idea to help me?

Hi. I would try and log out req.headers["Content-Type"] and remove the
ParseFormBody option from HttpServerSettings.options
(settings.options &= ~HttpServerOption.ParseFormBody;). Then you can
also log the request body using req.bodyReader.readAll() and see what
mootools actually sends.

The Content-Type should be "application/x-www-form-urlencoded" or
"multipart/form-data".

This framework is cool. Keep up the good work. There is any irc channel
where we can talk about vibe? Thanks.

AFAIK there is no IRC channel. Personally, I've never really used IRC
that much, which is the reason why I didn't set one up..

Sönke

Re: httpServerRequest params

Hi. I would try and log out req.headers["Content-Type"] and remove the
ParseFormBody option from HttpServerSettings.options
(settings.options &= ~HttpServerOption.ParseFormBody;). Then you can
also log the request body using req.bodyReader.readAll() and see what
mootools actually sends.

  • body (always look like this, with/without mootools & ajax, with/without settings.options &= ~HttpServerOption.ParseFormBody;):

returnTicket=true&classOfService=eco&departureAirport=PAR&arrivalAirport=ROM&departureDate.day=8&departureDate.monthYear=10-2012&returnDate.day=15&returnDate.monthYear=10-2012&adultCount=1&childCount=0&babyCount=0&flFormSubmit=Rechercher

  • params (empty)
  • query (empty)
  • cookie (empty)
  • form (empty)
  • header (see below ..)

The Content-Type should be "application/x-www-form-urlencoded" or
"multipart/form-data".

The header when i use mootools looks like this:

POST /recherche HTTP/1.1
Host: localhost:8080
Connection: keep-alive
Content-Length: 255
Origin: http://localhost:8080
X-Requested-With: XMLHttpRequest
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.1 (KHTML, like Gecko) Chrome/21.0.1180.89 Safari/537.1
Content-type: application/x-www-form-urlencoded; charset=UTF-8
Accept: text/html, application/xml, text/xml, /
Referer: http://localhost:8080/chercher
Accept-Encoding: gzip,deflate,sdch
Accept-Language: fr-FR,fr;q=0.8,en-US;q=0.6,en;q=0.4
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.3

Whitout mootools, it look like this (this is the one who actually work):

POST /recherche HTTP/1.1
Host: localhost:8080
Connection: keep-alive
Content-Length: 237
Cache-Control: max-age=0
Origin: http://localhost:8080
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.1 (KHTML, like Gecko) Chrome/21.0.1180.89 Safari/537.1
Content-Type: application/x-www-form-urlencoded
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,/;q=0.8
Referer: http://localhost:8080/chercher
Accept-Encoding: gzip,deflate,sdch
Accept-Language: fr-FR,fr;q=0.8,en-US;q=0.6,en;q=0.4
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.3

Hope this can help a bit :/

Re: httpServerRequest params

Content-type: application/x-www-form-urlencoded; charset=UTF-8

It was the additional "; charset=UTF-8" that caused the form decoder to ignore the request. I have pushed a fix for this to github master.