I'm pretty new with all these things but hope that you guys can help me understand how does it work. I got a form with field . How do i get data from client back? Was looking for some information but couldn't find.
My form:
<form action="/login" method="POST">
<input class="form-control" id="txt_memberid" name="txt_memberid" placeholder="MemberId" type="text" value="" />
<input class="form-control" id="txt_username" name="txt_username" placeholder="Username" type="text" value="" />
<input class="form-control" id="txt_password" name="txt_password" placeholder="Password" type="password" value="" />
<input id="btn_login" name="btn_login" type="submit" class="btn btn-default" value="Login" />
</form>
My main class:
get("/login", (request, response) -> {
ConsoleLog.consoleLog(log, request);
HashMap model = new HashMap();
model.put("content", new VelocityTemplateEngine().render(new ModelAndView(new HashMap(),"templates/form_login.vtl")));
return new ModelAndView(model, "templates/base.vtl");
}, new VelocityTemplateEngine());
post("/login", (request, response) -> {
ConsoleLog.consoleLog(log, request);
Integer memberId = -1;
String username = "";
String password = "";
try {
username = request.queryParams("txt_username");
password = request.queryParams("txt_password");
memberId = new Integer(request.queryParams("txt_memberid"));
} catch (Exception e) {}
log.info("MemberId="+memberId+" UserName="+username+" Password="+password);
if (new MemberDAO().isAuth(new Auth(username,password,memberId))) {
request.session(true);
request.session().attribute("txt_memberid", memberId);
request.session().attribute("txt_username", username);
}
return "process";
});
If I send form (method=get) - all works fine request.queryParams(XXXX) If I send form (method=post) - in request.queryParams - nothing
Maybe anyone know what I should do in POST request?
Wondering too. Are we supposed to parse the body manually?
Yes, I parse manualy
С уважением, Денис
21 нояб. 2015 г., в 20:32, Bas Roding [email protected] написал(а):
Wondering too. Are we supposed to parse the body manually?
—
Reply to this email directly or view it on GitHub.
If need, I send to you Class to parse
С уважением, Денис
21 нояб. 2015 г., в 20:32, Bas Roding [email protected] написал(а):
Wondering too. Are we supposed to parse the body manually?
—
Reply to this email directly or view it on GitHub.
I'm not sure I understand. I simplified your example a little:
get("/login", (request, response) -> {
return new ModelAndView(new HashMap<>(), "templates/base.vtl");
}, new VelocityTemplateEngine());
post("/login", (request, response) -> {
String a, b, c;
a = request.queryParams("txt_username");
b = request.queryParams("txt_password");
c = request.queryParams("txt_memberid");
return String.join(" AND ", a, b, c);
});
If you post this form, you will get everything back, what is it that you have to do manually?
I'm not sure sending username and password through http URL as query parameters is a good practice (http://stackoverflow.com/questions/2629222/are-querystring-parameters-secure-in-https-http-ssl).
I think it would be more secure to send them as body, so it won't be logged by any webserver.
So, spark does not support body payload mapping to pojos, doesn't it?
I solved this quickly and easily by using this tool (http://www.jsonschema2pojo.org/) to generate Java source for POJOs from JSON. Then I use the Gson parser to convert between the POJOs to JSON and back again. Works well for my needs.
For everybody running into this: Note that the body field in the Request object is lazily filled when .body() is called. I spend a few minutes wondering why the body field was empty in the debugger.
We can use Apache URLEncodedUtils class to parse the .body() as referred here: here
Most helpful comment
For everybody running into this: Note that the body field in the
Requestobject is lazily filled when.body()is called. I spend a few minutes wondering why the body field was empty in the debugger.